Feed on
Posts
Comments

Archive for the 'Matlab' Category

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

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

so easy: pbaspect()

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:

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

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

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

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

« Prev - Next »