Random Matlab Tips

  • Change histogram bar color. Truly displeasing how difficult this is:
    h=findobj(gca,'type','patch');
    set(h,'facecolor',[0.5 0.5 0.5],'edgecolor','w');  % change to gray
    
  • Inverting a colormap. Here is an example.
    colormap(flipud(gray));
    
  • Adding a simple unit label to colorbar: Funny how much time it took me to figure this out:
  • imagesc(rand(100,100)); 
    h=colorbar; 
    xlabel(h,'x string');   % for placement on one side
    ylabel(h,'y string');   % for other side
    

  • Making color scales constant: Using a function like imagesc to generate a color map on one figure. Now use cl=get(gca,'clim') to get the current color axis limits and save to vector cl. Now generate the second figure with imagesc. To make the color axis limits on plot 2 the same as on plot 1, do a set(gca,'clim',cl). Use colorbar to confirm. Example:
  • subplot(2,1,1); imagesc(rand(100,100)); cl=get(gca,'clim');
    subplot(2,1,2); imagesc(rand(100,100)); set(gca,'clim',cl);
    

  • Processing/plotting many data files in a loop: When dealing with lots of data, it pays to generalize your analysis software.
    files={
    'data-slow.txt',
    'data-medium.txt',
    'data-fast.txt'};
    for i=1:length(files)
      data=load(char(files(i));
      % process data
      % plot data
      plot(data(:,1),data(:,2)); 
      title(cat(2,'Plotting file #',num2str(i),' from file ',char(files(i))));
      pause;
    end
    
  • Making an AVI movie: The resulting movie plays correctly on Mac and PC/Windows based computers. However, the cost for this compatability is a very large movie file (relative to that of other formats).
    files={
    'data1.txt',
    'data2.txt',
    'data3.txt'};
    fig1=figure(1); set(gca,'Position',[0.13 0.11 0.775 0.515368]);
    winsize=get(fig1,'Position');
    winsize(1:2)=[0 0];
    numframes=i2-i1+1;
    A=moviein(numframes,fig1,winsize);
    for i=1:numframes
        set(gca,'nextplot','replacechildren');
        data=load(char(files(i)));
        imagesc(data);
        title(cat(2,'Result for ',char(files(i))));
        A(:,k)=getframe(fig1);
        pause(0.02);
     end
    outfile=cat(2,'plotstacks_movie_',stackinfile,'.avi');
    movie2avi(A,outfile,'fps',5);
    

  • Maximizing your figure window for Portrait: Note if you do this, you will have difficulty showing it in Powerpoint since Powerpoint slides are in "Landscape" mode.
    set(gcf,'PaperPosition',[0.25 0.25 8.25 10.75])
    

  • To generate a random number vector of length n between i1 and i2 without edge effects:
    irand=floor(i1+(rand(n,1)*(i2-i1))); 
    hist(irand,[i1:i2]); axis tight; % now inspect histogram
    
  • Finding 5% and 95% PDF percentiles:
    tim=linspace(tim1,tim2,ntim);   % bins
    c2=hist(mparm(:,2),tim);
    cs2=cumsum(c2); 
    cs2=cs2./cs2(end); 
    tmp=abs(cs1-0.025); [tmp2,i1]=min(tmp);  % near end
    tmp=abs(cs1-0.975); [tmp2,i2]=min(tmp);  % far end 
    %result is 95% confidence region between indices i1 and i2
    

    Revised: Wednesday, 11-Nov-2009 11:05:14 PST