"MATLAB" MATLAB programming Learning Notes "to be Continued" __ Programming

Source: Internet
Author: User
Tags dashed line function definition first row

Recently want to use MATLAB data analysis, algorithm performance test, usually because of the use of C, C + +, so a lot of habits are not changed, here to list some of the MATLAB in the obvious different places. matrix element access mode : A (---a[1][2] Select a row or column of a matrix: A (:, 1);% Select the first column of the Matrix. A (1,:);% Select the first row matrix cell and array start subscript: The row and column are all starting from 1, a (n) is the first element a (0,1) is out of bounds corresponding to a (1,columns) non-crossed MATLAB matrix storage is the column first , and our usual is the line priority. For example, A is a matrix of 2 rows and 2 columns. A (2) represents a (2,1) instead of a (a) operation symbol: ~ denotes. , | & the corresponding representation | | && string representation : Single quotation mark ", and C is double quotation mark" ", so when making a character comparison, you cannot use c = = ' \ n ' to know that ' \ n ' represents a string. In this case, you need to replace the ASCII code. c==10 conversion Function: string to numeric Str2Num (...) MATLAB matrix format can be: [1,2\n3,4\n5,6\n], so in the programming of data generation, we can generate experimental data in this format, and then directly copied into MATLAB can be implemented matrix assignment. This makes it cumbersome to use file reads.

Here is an M file that I wrote for the first time: The function is to read the formatted data from the text data

My.txt text content:

The implementation code is as follows: READ.M

%%write a program for reading files% fid=fopen (' my.txt ', ' R ');
 %%set j=1 Notice:matlab First index is 1 not 0.
 % I=1;
 J=1;
 flag=0;
     While ~feof (FID) s=fgets (FID);
     Temp= "; For K=1:length (s)%| |   
                 (S (k)) ==eof if (S (k) ==32 | s (k) ==10)% ' ten ' \ n ' if flag==1%last char is not '
                 %fprintf (1, ' temp=%s\n ', temp);
                 A (i,j) =str2num (temp);
                 %fprintf (1, ' a[%d][%d]=%f\n ', I,j,a (i,j));              
             J=j+1;%add columns temp= ";
         End flag=0;
             else%connect the new char to temp temp (length (temp) +1) =s (k);
         flag=1;
 End end percent I=i+1;%add rows J=1;%re set j=1 Notice:matlab First index is 1 not 0.
     End If Flag==1 [Rows,columns]=size (A);
     A (rows,columns) =str2num (temp);
     %fprintf (1, ' a[%d][%d]=%f\n ', I-1,j,a (i-1,j));
     flag=0;
 Temp= ";
 End fclose (FID); A

In addition: Str= ' \ n ' then str (1) =\ str (2) =n instead of STR (1) =\n line break. The ASCII code 10 is used instead of the actual use. Other characters this is not required.

str= ', ', replaced by STR (1), similar to other general characters can do so.

m file writing, function call

function definition form: function[output variable list]= function name (input variable list)

function body language;

Note: When more than one input variable, you should use square brackets, more than one input variable should be separated by commas, after the writing must be saved with the function name, otherwise cannot be called, the function m file cannot access the workspace

the variable. Example:One output variable the output variable

%FILENAME:SCI.M% FILENAME:SC.M

                    function Y=sci (x)                                                                 FUNCTION[Y,Z]=SC (x)

..............                                                                         .........

Basic Drawing Control

You can specify color, Linetype, and data point icons when you call plot, in the basic format: plot (x, y, ' color-linestyle-marker ')

When you specify only the data point icon, the data points are not connected to a line, and only the isolated data points are drawn. The string parameters are evaluated as follows:

Color: Y (yellow); R (red); G (green); B (blue); W (white); K (black); m (violet); C (Cyan)

Linear:-(solid line);: (dotted line);-. (dashed);--(dashed line).

Data point icon:.; +;*;o (Small circle);p Entagram (Pentagram) and so on,

The size, shape, padding, etc. of these markers can be viewed in more detail: http://blog.csdn.net/benjmzhu/article/details/7246892

Plot (T,sin (2*t), '-mo ',...
' LineWidth ', 2,...
' Markeredgecolor ', ' K ',...
' Markerfacecolor ', [. 1.],...
' Markersize ', 12)

Coordinate system control: When not specifically specified, MATLAB automatically specifies the horizontal ordinate scale of the graphic and the range of display, if not satisfied, can be controlled by the axis command, commonly used are:

Axis ([xmin xmax ymin ymax]) [] gives the minimum and maximum values for the x and y axes, respectively

Axis equal x axis and Y axis are the same unit length

Axis Square Chart Box square

Axis off Cancel axis

graphic callouts

Xlabel,ylabel,zlabel,title

Text and Gtext are used to add strings to a particular position in the drawing, where the position of the string is specified in the command, and the latter is specified with the mouse;

The grid adds a grid to the graph.

multiple Images : Subplot (m,n,p) can draw different images in the same graphics window.

three-dimensional graphics

Spatial curve: plot3 (x, Y, z)

Surface with mesh: [X,y]=meshgrid (x, y); mesh (x, y, z); Surf (x, y, z)

High Line: Two-dimensional three-dimensional high-line function contour,contour3

Open another graphics Window command: Figure (n)

MATLAB programming on-Line data collection:

Matlab General graphics function command Matlab basic drawing function http://wenku.baidu.com/view/f451ff84bceb19e8b8f6ba8f.html

Matlab set the font and line size in the diagram, etc. http://wenku.baidu.com/view/9f865323aaea998fcc220e5e.html

Cond......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.