Sunday, September 27, 2009

Wednesday, September 23, 2009

Copy File using command/script in Matlab

This one comes handy when you have repetitive work to copy the files to some directory.

I had to conduct different tests with different parameters and hence wanted an automated way to clean up the files after each test. I wanted to have them organized in folder as per attribution.



This is how I copied my file:
Untitled.m into the folder

mkdir destination_folder
[s,mess,messid]=copyfile('Untitled.m','destination_folder')

see help copyfile feature for details.


This is quite stunning to realize that Matlab has unix features such as ls and pwd
while not cp or mv for copying the files. I started by assuming that cp or mv should work. It does not!
After I copy the files, I can:
delete('Untitled.m')

%%%%%%%%%%%%%%%%%%%%%%%%%%-------------%%


function clean_up_fig

jj = 'test';
%%
cd G1_OK
mkdir(jj)

copyfile('*.fig',jj)
copyfile('*.tif',jj)
copyfile('*.mat',jj)

delete('*.tif')
delete('*.fig')

cd ..



Sunday, September 20, 2009

Get desired elements from the given array in Matlab

A very simple function to collect the items from the array which meets the required condition




clear all
A=[1,2,3,4,5,6,7,8,9,10];
B=zeros(1,length(A));
k=1;
diff = A(1);
B(1) = diff;
for i=2:length(A)
diff=diff+3;
    B(i) = diff;

end
B = B(B<=max(A))

Tuesday, August 18, 2009

Get the desired number of samples

I was wondering how to get n number of sample outputs constrained by the situations given.
AB > C^2
accept C
else reject C
however, I need n non zero final samples.

Wrote this one:
It works for me!





function Obj = get_random_v3(Obj, n)

% n=250

j=1;



v = zeros(1,n);



notOK3 =1;

while notOK3

Obj(j).v1 = rand();

Obj(j).v2 = rand();

Obj(j).v3 = rand();



if (Obj(j).v1*Obj(j).v2) > ((Obj(j).v3)^2)

v(j) = Obj(j).v3;

j=j+1;

end

v(v == 0) = [];

if (length(v) == n) %&& (length(v6) == n) && (length(v9) == n) && (length(v12) == n)

notOK3=~notOK3;

end

end

Tuesday, June 16, 2009

do till condition is met "continue" example

This is a nice example.



clear

y = 1:100;
count = 0
for j = 1:length(y)

if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end



This takes x until y(j)>50. After the condition is met it simply passes out.


Better explained by:
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end

continue keeps on taking values does not break the chain.
while break breaks it and comes out of the loop



Contrasted over the
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end


lastly;


clear
clc
y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)<20||y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end

this does not even work while continue will keep looking for the situation.
Break just breaks while continue keep working escaping to the next one and checking!

Thursday, June 11, 2009

saving .mat file as per the iteration

I wanted to save the mat files as the iteration progressed.
here is the quick solution:
it saves the mat file with number of iteration as the file name: viz: 100.mat, 200.mat etc.
This is handy when you want to compare the results of the intermediate results as you progress the iteration.





if rem(nn,SS/10)==0
disp(['iteration: ', num2str(nn), ' of ', num2str(SS)]);
save ([num2str(nn)])
end