Matlab Tips

How to extract contours from contour function

All the information about the contours is stored in parameter c

[c,h] = contour(AC,BC,REG,[2:6 8 10]);

We extract x and y values of the contour in the loop and plot it, e.g.

flag = 1; % condition
stat = 2; 
endt = c(2,1)+1;
while flag==1
    x = c(1,stat:endt);
    y = c(2,stat:endt);
    plot(x,y,'r')
  try
    stat = endt+2;
    endt = endt+1+c(2,endt+1);
  catch
    flag = 0;
  end
end

Then, we remove the original contours

delete(h)

Format a string using ‘num2str’

Converting numbers to string sometimes requires using certain number of digits. The string can be formatted using num2str function, e.g.,

iter = num2str(1,'%.4d')
iter = 
  0001

Fixed point notation indicating a number of digits to the right of the decimal point requires the following:

num2str(pi,'%.4f')
ans = 
  3.1416

If we need to use other notation such as exponential, then:

num2str(0.001,'%.2e')
ans = 
  1.00e-03

Custom colormap

Type in the command window

colormapeditor

Apply changes and save the current colormap in a variable and in the MAT-file:

mycmap = colormap(gca);
save('MyColormaps','mycmap')

In other sessions, first  load the colormap into the workspace and then assign it to the figure:

load('MyColormaps','mycmap')
set(gcf,'Colormap',mycmap)

Rendering graphics

Rendering method used to draw faster and more accurately (but it can consume lots of system memory)

set(gcf, 'renderer', 'zbuffer')

Matlab to Illustrator

Save figure in Matlab in order to be editable in Illustrator

print -depsc2 -tiff file_name.eps

Evaluate string as Matlab expression

If you have a string expression e.g. ‘sin(2)’, you can execute it using eval function

>> A = 'sin(30)';
>> eval(A)
ans =
 -0.9880

to be continued