Feed on
Posts ## parse a text file in matlab

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

## desaturate colors in matlab

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])); Â  Â

## force a number of tick marks in a matlab plot

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])

## change a matlab plot’s aspect ratio

so easy: pbaspect()

## put vector into matlab figure legend

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:

## use linear algebra to fit a line in matlab

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 […]

## generate pure tones in matlab

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

## make high-quality, publication-level figures in matlab

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, […]

## graphics/private/name errors when printing matlab plots

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’);

## how to make a transparent histogram in matlab

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′);