Matlab programming learning notes [To be continued]

Source: Internet
Author: User

Recently, I want to use Matlab for data analysis and algorithm performance testing. Because I usually use C and C ++, many of my habits cannot be changed at the moment, here we will list some obvious differences in Matlab.

  1. Matrix Element access method: A (1, 2) --- A [1] [2]
  2. Select A row or column in The Matrix: A (:, 1); % select the first column of the Matrix. A (1, :); % select the first row of the matrix
  3. Matrix units and ArraysStart subscript: Rows and columns start from 1. If A () is the first element A (), the corresponding A (1, columns) does not cross the border.
  4. What is Matlab matrix?Column priorityAnd we usually take the line first. For example, A is A matrix of two rows and two columns. A (2) represents A () rather than)
  5. Operator number :~ Represents! , | & Corresponding representation | &&
  6. String Representation: Single quotes '', while double quotes" in C. Therefore, when comparing characters, c = '\ n' cannot be used. You must know that' \ n' represents a string. In this case, it must be replaced by an ASCII code. C = 10
  7. Conversion Function: string to str2num (...)
  8. The matrix format of Matlab can be [1, 2 \ n3, 4 \ n5, 6 \ n]. Therefore, when generating data by programming, we can generate experimental data in this format, then, you can directly copy it to Matlab to assign values to the matrix. In this way, you do not need to use files for reading.

Here is a M file written for the first time: the function is to read formatted data from text data.

My.txt text content:

 

The implementation code is as follows:

Read. m

 1 %
2 %write a program for reading files
3 %
4 fid=fopen('my.txt','r');
5 %
6 %set j=1 notice:matlab first index is 1 not 0.
7 %
8 i=1;
9 j=1;
10 flag=0;
11 while ~feof(fid)
12 s=fgets(fid);
13 temp='';
14 for k=1:length(s) %|| (s(k))==EOF
15 if (s(k)==32 | s(k)==10) % ''10 % '\n'
16 if flag==1 %last char is not ''
17 %fprintf(1,'temp=%s\n',temp);
18 A(i,j)=str2num(temp);
19 %fprintf(1,'A[%d][%d]=%f\n',i,j,A(i,j));
20 j=j+1;%add columns
21 temp='';
22 end
23 flag=0;
24 else
25 %connect the new char to temp
26 temp(length(temp)+1)=s(k);
27 flag=1;
28 end
29 end
30 %%
31 i=i+1;%add rows
32 j=1;%re set j=1 notice:matlab first index is 1 not 0.
33 end
34 if flag==1
35 [rows,columns]=size(A);
36 A(rows,columns)=str2num(temp);
37 %fprintf(1,'A[%d][%d]=%f\n',i-1,j,A(i-1,j));
38 flag=0;
39 temp='';
40 end
41 fclose(fid);
42 A

In addition: str = '\ n', str (1) = \ str (2) = n instead of str (1) = \ n line break. Therefore, the ASCII code 10 is used in actual use. Other characters are not required.

Str = ',', replaced by str (1), similar to other general characters.

Write M Files and call Functions

Function Syntax: function [output variable list] = function name (input variable List)

Function language;

Note: When there are more than one input variable, square brackets should be used. When there are more than one input variable, they should be separated by commas (,). After compilation, the disk must be saved by the function name; otherwise, it cannot be called, the function M file cannot access the workspace.

. For example:One output variable two output variable

% Filename: sci. m % filename: SC. m

Function y = sci (x) function [y, z] = SC (x)

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

Basic Drawing Control

When you call plot, you can specify the color, line type, and data point icons. The basic format is: plot (x, y, 'color-linestyle-marker ')

When only the data point graph is specified, the data point is not connected to a line, but only isolated data points are drawn. The value of the string parameter is as follows:

Color: y (yellow); r (red); g (green); B (blue); w (white); k (black); m (purple ); c (green)

Line type:-(solid line); (DOT line);-. (virtual line);--(dotted line ).

Data point icons:.; +; *; o (small circle); pentagram (pentagram), etc,

The size, shape, fill, and other details of these markers can be viewed: http://blog.csdn.net/benjmzhu/article/details/7246892

 

Plot (t, sin (2 * t), '-Mo ',...
'Linewidth', 2 ,...
'Markeredgecolor', 'k ',...
'Markerfacecolor', [. 49 1. 63],...
'Markersize', 12)

 

Coordinate System Control: matlab automatically specifies the ratio of horizontal and vertical coordinates and the display range of the image, if not satisfied, can be controlled by the axis command, commonly used include:

Axis ([xmin xmax ymin ymax]) [] returns the minimum and maximum values of the X and Y axes respectively.

The length of the axis equal X axis and Y axis is the same.

Axis square

Axis off cancel axis

Graphic tagging

Xlabel, ylabel, zlabel, title

Text and gtext are used to add a string to a specific position in the image. The position of the former string is specified in the command, and the latter is specified with the mouse;

Add a grid to the graph.

Multiple images: Subplot (m, n, p) can be used to draw different images in the same graphic window.

3D 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, limit 3

Open another graphic window command: figure (n)

Matlab programming online data collection:

Http://wenku.baidu.com/view/f451ff84bceb19e8b8f6ba8f.html of basic plotting functions of MATLAB universal graphic function commands

Matlab sets the font and line size http://wenku.baidu.com/view/9f865323aaea998fcc220e5e.html in the graph

 

To be continued ......

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.