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 No Comments »
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 this […]
Posted in Matlab on April 30th, 2007 No 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 No 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, however: […]
Posted in Matlab on April 22nd, 2007 No Comments »
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 No 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′);
Posted in Matlab on July 18th, 2006 No Comments »
let’s say you’ve got y = f(x), where x and y are both matlab arrays.
to numerically integrate f(x), just use the command trapz:
>> area_under_the_curve = trapz(x,y);