Thursday, April 15, 2010

save figure as per iteration in matlab

I wanted to save the files in jpeg format as I go...
The following helped me to save it in myplot1.jpeg format.



saveas(gcf,['myplot', num2str(iteration)], 'jpeg')
or

print(gcf, '-djpeg',['Entropy_map_',num2str(j)])

Wednesday, April 14, 2010

Sampling within the range

Explore and get me values that I want within the range

close all
clc
clear all
m = 23;
n = 34;
jumpSTEP = 11;
broken = 1;
playX = [-3:0.1:3];
playY = [-3:0.1:3];
reject = 1;
for jj = 1:1000
    notOK = 1;
    while notOK
        m = m + ceil(jumpSTEP*rand);
        n = n + ceil(jumpSTEP*rand);
        m = rem(m,length(playX));
        n = rem(n,length(playY));
        saveM(jj) = m;
        saveN(jj) = n;
        if m<1
            reject = reject+1;
            continue
        end
        if n<1
            reject = reject+1;
            continue
        end
        if m>1
            notOK =0;
        end
        if n>1
            notOK =0;
        end

    end
end
figure
plot([1:1000],saveM, 'or');
hold on
plot([1:1000],saveN, 'ok');
plot([1:1000], 1, [1:1000], length(playX) );
min(saveM)
min(saveN)


Saturday, April 10, 2010

Unix commands inside matlab: cp, mkdir, rm etc

Implementing the unix commands in the matlab is so easy.
I wanted to automate the file organizing process.
A very simple code combined with http://usefulcodes.blogspot.com/2010/03/recursive-action-into-folders.html is going to work great!
unix('mkdir back') % make directory for backup

unix('cp *.m back') % copy to it or may be more would be better
unix('cp  114.mat back') % copy the file to be saved
unix('cp *.jpg back') % copy the figure to be saved
unix('rm *.*') % delete all files :) not directory

Sunday, April 4, 2010

Nested Function inside function

I had never done a nested function inside another.
It helps to avoid the necessity of having more files as m function. Useful when the function is only called by the same file.
However, the main file itself needs to be saved as function instead of script.


function test
home
a = 1;
b = 2;
test1 = istested(a,b)

function answer = istested(a,b)
a = a+1;
answer = isequal(a,b);
return