Download the full version of Guide: http://www.armbbs.cn/forum.php?mod=viewthread&tid=94547
Chapter 4 Matlab simple use of script files
This issue tutorial is simple m file to explain the use of Matlab, and some content to keep up with the same one, but more detailed than some.
4.1 Important Beginners
4.2 Matlab .m file scripts use
4.3 Matlab function of cycle condition and
4.4 graphics
4.5 summary
4.1 Beginner important to mention
- This chapter before the holiday, be sure to priority learning Chapter 3.
- For use Matlab m-file, be sure to master, subsequent chapters are based on the m-file to do the test.
4.2 Matlab .m file scripts use
Creating and using the matlab .m files with the creation and use of MDK or IAR above .C or .ASM file is the same. Created as follows:
Click the image above the small icon, open the Edit window, enter the following function:
r = rand(50,1); plot(r)
We need to save the current file After editing functions:
Then click on the following icon to run (or press F5):
Display as follows:
4.3 Matlab function of cycle condition and
matlab also supports C-like language conditions and loops: for, while, if, switch. But in matlab use more casual than in C.
- For example, enter the following function in .M file:
nsamples = 5; npoints = 50; for k = 1 : nsamples currentData = rand(npoints,1); sampleMean(k) = mean(currentData); end overallMean = mean(sampleMean)
In the Command window to get the output:
- For each iteration the result of the above functions are output method may be employed:
nsamples = 5; npoints = 50; for k = 1:nsamples iterationString = ['Iteration #',int2str(k)]; the DISP (iterationString) % Note that there is no semicolon, so as to ensure the output will be in the command window currentData = rand(npoints,1); sampleMean (K) = Mean (CurrentData)% Note that no semicolon end overallMean = Mean (sampleMean)% Note that no semicolon
In the Command window to get the output:
- If you add the following sentence following the above function:
if overallMean < .49 disp('Mean is less than expected') elseif overallMean > .51 disp('Mean is greater than expected') else disp('Mean is within the expected range') end
Command window output results are as follows (listed here only the last three rows):
4.4 graphics
4.4.1 The basic plot function
- Depending on the plot of input parameters, mainly in two ways:
- Plot (y), in this way, it is primarily to produce a linear curve according to the number of data y.
- plot (x, y) is the x-coordinate axis plotted.
For example, write the following function in the command window or .m file:
x = 0 : pi / 100 : 2 * pi; and = sin (x); plot(x,y) xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function','FontSize',12)
- The following function can display a plurality of curves on a picture.
x = 0 : pi / 100 : 2 * pi; and = sin (x); y2 = sin (x. 25 ); y3 = sin (x. 5 ); plot(x,y, x,y2, x,y3) legend('sin(x)','sin(x-.25)','sin(x-.5)')
- Further style and color profile can be configured, the command format is as follows:
plot(x, y, 'color_style_marker')
Here a few examples to look at the actual display.
x = 0 : pi / 100 : 2 * pi; and = sin (x); plot(x,y,'ks')
Display as follows:
Display the following functions:
x = 0 : pi / 100 : 2 * pi; and = sin (x); plot(x,y,'r:+')
The following display functions as follows:
- Complex plot
Plot draw only real function of the default data unit, if it is below this form, the real and imaginary part are drawn. plot (Z) is the plot (real (Z), imag (Z)). Now we realize the following function function in the command window:
t = 0 : pi / 10 : 2 * pi; plot(exp(i*t),'-o') axis equal
Display as follows:
- Add a new function in the current plot drawing
Hold on to use the function to achieve this function we have used in the previous section, the effect is to add a new drawing on the basis of the current drawing.
% Obtain data from evaluating peaks function [x,y,z] = peaks; % Create pseudocolor plot pcolor(x,y,z) % Remove edge lines a smooth colors shading interp % Hold the current graph hold on % Add the contour graph to the pcolor graph contour(x,y,z,20,'k') % Return to default hold off
Display as follows:
- Axis Setting
- Visibility Settings
axis on% set visibility
OFF Axis % setting is not visible
-
- Grid Settings
grid on% set visibility
OFF Grid % setting is not visible
-
- Setting the aspect ratio
axis square% set X, Y-axis as long as equal Axis % set X, Y of the same increments. Auto Normal Axis % automatic mode is set.
-
- Setting the axis limits
axis([xmin xmax ymin ymax]) %二维 axis([xmin xmax ymin ymax zmin zmax]) %三维 Auto Axis % automatically provided
4.4.2 rendering image data
The following example illustrates a simple click of rendering image data, the operation at the command window.
>> load durer >> whos Name Size Bytes Class Attributes X 648x509 2638656 double ans 648x509 2638656 double caption 2x28 112 char map 128x3 3072 double >> image (X)% display pictures
>> colormap (map)% color
>> axis image% set coordinates
Using the same method, you can load the picture detail operation. In addition users can use the function imwrite imread and operating standards JPEG, BMP, TIFF and other types of pictures.
4.5 summary
In this issue with you on the simple use of Matlab, you need to check the manual and more, practice more.