Passion * Hope * Dream

小林的百科筆記

Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Just now found this nice short piece of code for drawing a simple 5-pointed star:

>> theta = 0:4/5*pi:4*pi;
>> figure;
>> plot(cos(theta),sin(theta),'m-')
>> axis equal; axis tight




Source: http://www.mathworks.com/matlabcentral/newsreader/view_thread/130807

There is no built-in function in Matlab to round a number to specific significant figures. However I have found a little trick from somewhere in the internet which works! It simply makes use of the 'mat2str' (or 'num2str') and 'eval' (or 'str2num') Matlab functions. For example, first generate a matrix 'A' with random numbers:

A = rand(2,3)*100

A =
70.6046 27.6923 9.7132
3.1833 4.6171 82.3458

Then convert into 'string' using 'mat2str' and specifies how many digits of precision, here I have made this to be 3:

>> A_temp = mat2str(A,3)

A_temp =
[70.6 27.7 9.71;3.18 4.62 82.3]

Now convert the string back to numerical values by using 'eval':

>> A_rounded = eval(A_temp)

A_rounded =
70.6000 27.7000 9.7100
3.1800 4.6200 82.3000


So the values have been rounded to 3 sig. fig. :-)

Now if we use 'num2str' instead of 'mat2str', we need to convert back to numerical values by using 'str2num' because 'eval' will not work. Moreover, I just found out that using 'num2str' can actually allow rounding to specific decimal places. Using the same 'A' matrix as above, and then specify the precision to be '%4.4e\n' which will round the values to the 4th decimal place:


A_temp2 = num2str(A,'%4.4e\n')

A_temp2 =
7.0605e+001
2.7692e+001
9.7132e+000
3.1833e+000
4.6171e+000
8.2346e+001

>> A_rounded2 = str2num(A_temp2)

A_rounded2 =
70.6050
27.6920
9.7132
3.1833
4.6171
82.3460

Noticed that it is necessary to have the '\n' in the string that specifies the format of precision, otherwise the result will go weird.

PS: I noticed that there are still small rounding errors (or floating point errors), however this trick should be good enough for general purposes.

Matlab only supports 7 color strings, which are:
'blue', 'green', 'red', 'cyan', 'magenta', 'yellow' and 'black'.

But very often we need to plot more than 7 data sets in one single plot. One solution is to repeat the colours and then use another type of marker. However it is desirable to have more than 7 colours for plotting. This is possible by using the built-in colourmaps in Matlab. For example:

x = -pi:.1:pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(x).*cos(x);
y4 = 2*sin(x);
y5 = 2*cos(x);
y6 = sinh(x);
y7 = cosh(x);
y8 = tanh(x);
y9 = (sin(x)).^2;
y10 = (cos(x)).^2;

% now generate 10 different colours from blue to red
colorRange = jet(10);

figure;
plot(x,y1,'Color',colorRange(1,:));
hold on; plot(x,y2,'Color',colorRange(2,:));
hold on; plot(x,y3,'Color',colorRange(3,:));
hold on; plot(x,y4,'Color',colorRange(4,:));
hold on; plot(x,y5,'Color',colorRange(5,:));
hold on; plot(x,y6,'Color',colorRange(6,:));
hold on; plot(x,y7,'Color',colorRange(7,:));
hold on; plot(x,y8,'Color',colorRange(8,:));
hold on; plot(x,y9,'Color',colorRange(9,:));
hold on; plot(x,y10,'Color',colorRange(10,:)); legend('y1','y2','y3','y4','y5','y6','y7','y8','y9','y10','Location','NorthEastOutside'); axis tight;

The code above used the 'jet' colormap. Other built-in colormaps in Matlab are: 'autumn', 'bone', 'colorcube', 'cool', 'copper', 'flag', 'gray', 'hot', 'hsv', 'line', 'pink', 'prism', 'spring', 'summer', 'white', and 'winter'.