Friday, November 27, 2009

Print date and time: matlab

Wanted to learn what time the program was finished on the middle of the night or something like that...


having
 datestr(now)
at the end of the program helps to print out the date and time.

Wednesday, November 25, 2009

Matlab script for figure with background color

This is the Matlab script to make a white circle with dark background.

The figure1 creates a figure with dark background as indicated by the color attribute.
patch is a built in function which creates a light patch (which is a circle).

I tried the built in function "whitebg" which did not work except that it created another color background (viz: whitebg('green') gives me green background without solving the problem).
set(subplot(2,2,1),'Color','Red') actually sets the color for the subplot but I could not make it work for single figure.
Listed in Matlab file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/25935
Here is the code:

The file gives a circle of given radius with dark background.
generate x and y vectors for circle.

figure1 = figure('Color',[0 0 0]);

axes1 = axes('Parent',figure1,'PlotBoxAspectRatio',...
[1 1 1],'Color',[0 0 0]);
% box('on');
patch(x,y,[1,1,1])
axis square


Note that the point here is not to generate shape, it can be done with conversion into polar co-ordinates (x,y) into (r,theta). My aim was to have different background for figure.

Thursday, November 12, 2009

Edit the Grub boot loader Menu.lst in Ubuntu

Everytime I upgrade the Ubuntu package, I come across the choice of keeping the boot loader. I try to keep the menu.lst updated by accepting the changes.
This has a consequence: The default value on the menu.lst makes computer boot into another version of Ubuntu.
For example if I had XP/Vista OS as Third Option (selection 2) then after changes it would just start whatever is on the third option.
It could be Ubuntu 9.10, kernel 2.6.x.x.-generic

So, I need some way to edit the menu.lst or the boot loader or the grub menu editor.

How do I do it?

start the terminal
type:
gksudo gedit /boot/grub/menu.lst
or
sudo vi /boot/grub/menu.lst
remove the un-necessary items on the list.
save it and restart.

Thursday, October 1, 2009

How to insert javascript codes in blogger

Inserting the javascript code into the blogger template itself is very good idea.

  1. no need to search for extra place to look for uploading code.
  2. Easy to manage
  3. fun
  4. ...
So, in the section do this:


<script type='text/javascript'>

//<![CDATA[

PASTE THE JAVASCRIPT CODE HERE

//]]>

</script>

converted to code by using...
http://www.eblogtemplates.com/blogger-ad-code-converter/

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


Friday, March 20, 2009

How to show the highlighted content in the blogger post

I wanted to highlight certain texts in the blogger posts, mostly codes and others and was having difficulties.

I just looked into the source code of one of the blog and realized that this piece of code will work:


test for a table style highlights (contents here)



So, I can show the highlighted content in the blogger post

Monday, March 16, 2009

Use of figure handle in Matlab

I used to run the plots in matlab without having any figure handle, but this morning I realized that if I do not put the handle it wont display the picture correctly.
Somehow the figure handle is not coping with the compiz!


figure without handle



Disable compiz if such problems persist.

Friday, February 20, 2009

Playing with wav file in Matlab

To do:

READ THE AUDIO FILE
DISPLAY WAVEFORM
COMPUTE AND DISPLAY FREQUENCY DOMAIN AS FIGURE

% tHE VALIDITY YET TO BE TESTED...
clear all;


close all;


clc;


% import a audio

[y,Fs,nbit]=wavread('your_audio.wav');


% extract left channel only


left=y(:,1);


% calculate the time scale


Ts=1/Fs;


[nsamples,c]=size(y);


tscale=0:Ts:(nsamples-1)*Ts;


%plot left and right channel in time domain


plot(tscale,left,'r');


xlabel('Time (sec)');


ylabel('Amplitude');


%compute the FFT with the proper sampling frequency


fmax=Fs;


%how many points the FFT must be computed on?


N=nsamples;


tscale=0:Ts:(N-1)*Ts;


%N=number of FFT points:


% change it and test the different resolution


fscale=linspace(0,fmax/2,floor(N/2));


spectrum=fft(left,N);


%plot the spectrum of the signal using the appropriate


% frequency scale and module of the coefficients


%plot the magnitude


figure(2);


plot(fscale,abs((spectrum(1:length(fscale)))));


xlabel('Frequency Hz')


ylabel('Module')