Posted in Matlab on October 21st, 2008 2 Comments »

to parse a text file in matlab that can’t be read using the ‘load’ command, you can use fgetl in the following manner: fid = fopen(‘file.name’); while 1 tline = fgetl(fid); if ~ischar(tline) break end tline end

Posted in Matlab on April 14th, 2008 No Comments »

it’s surprisingly easy to desaturate colors in matlab. Â the trick is to use the HSV (hue, saturation, value) transformation to do so. Â for instance, to desaturate the color blue ([0 0 1] in RGB-speak) by 25%,Â use the following commands: >> desat = [1 0.75 1]; >> blue_desat = hsv2rgb(desat.*rgb2hsv([0 0 1])); Â Â

Posted in Matlab on February 20th, 2008 1 Comment »

matlab will automatically calculate how many tick marks produce the most pleasing axes in a plot.Â however, if you know better, you can override these settings by commanding: >> set(gca,’XTick’,[1:tick_number])

Posted in Matlab on February 20th, 2008 No Comments »

Posted in Matlab on February 1st, 2008 No Comments »

let’s say you’ve got a bunch of lines in a matlab plot whose labels live in a vector. to place all of those labels into the same figure legend, you can do the following: >> vec vec = 1 2 3 4 5 >> legend([repmat('penalty = ',length(vec),1) num2str(vec)]) the result:

Posted in Matlab on October 16th, 2007 1 Comment »

let’s say you’ve got 2 vectors (x,y) who are linearly related according to the relation: y = mx + b. you’d like to use matlab to find m and b. this can be done with one line of linear algebra in matlab. first, consider the equation y = mx + b: you can imagine rewriting […]

Posted in Matlab on April 30th, 2007 2 Comments »

save the following code to an m-file and run in matlab to generate some pure tones in wav format. % pure tones clear freqs = 500; duration = 100; for i = 1:length(freqs) sampleFreq = 44100; dt = 1/sampleFreq; t = [0:dt:duration]; s=sin(2*pi*freqs(i)*t); sound(s,sampleFreq); % wavName = sprintf(‘tone%d.wav’,freqs(i)); % wavwrite(s,sampleFreq,16,wavName); end

Posted in Matlab on April 22nd, 2007 4 Comments »

i had always been irked by how easy it was to make figures in matlab, but how crummy they looked when saved.Â i felt slightly embarrassed handing in such ugly figures for my homework; a journal editor would be seized with anaphylactic shock if i tried submitting matlab figures. i’ve finally stumbled upon the solution, […]

Posted in Matlab on April 22nd, 2007 1 Comment »

after upgrading to matlab 7.0, i started getting the following error messages when saving figures: >> saveas(gcf,’me.jpg’,’jpg’); ??? Error using ==> print Error using ==> graphics/private/name Cannot create output file ‘me.jpg’ Error in ==> saveas at 140 print( h, name, ['-d' dev{i}] ) the solution: write full filenames in saveas: >> saveas(gcf,’/Users/me/me.jpg’,’jpg’);

Posted in Matlab on July 18th, 2006 15 Comments »

to make a partially transparent histogram in matlab, use the facealpha setting. for instance, the following code produces the histogram seen above: figure hist(data1,20) h = findobj(gca,’Type’,’patch’); set(h,’FaceColor’,’r’,’EdgeColor’,’w’,’facealpha’,0.75) hold on hist(data2,20) h = findobj(gca,’Type’,’patch’); set(h,’facealpha’,0.75); ylabel(‘counts’) xlabel(‘gene-tree-length/species-tree-length’) legend(‘no HGT’,’HGT’) title(‘p = 0.5′);