Some Questions about MATLAB plotting

Source: Internet
Author: User
Tags sprintf

I saw a piece of news in Beijing and thought about how to test two-way mirrors? Baidu.

Just do the following simple test: Put your nail tip on the mirror surface, if there is a gap between the nail tip and the mirror image, it is the real mirror. However, if your nail tip can directly touch the nail reflection image, be careful! That's a two-way mirror!

1. How to display the coordinate of a point in MATLAB?

  • Show coordinate points in the cursor

There is a "Data cursor" button in the toolbar. click the button and the arrow turns to a small cross. The cursor will display the coordinates.

  • String function processing

  1: x=1+4*rand(1,5);
  2: y=2+2*rand(1,5);
  3: plot(x,y,‘o-‘)
4: % text (X, Y, S) indicates that the string s,
  5: for i=1:length(x)
  6:     text(x(i),y(i),[‘(‘,num2str(x(i)),‘,‘,num2str(y(i)),‘)‘])
7: % add text at (x, y). The text content is (x, y)
8: % x and y at each point use num2str to convert the number into a string.
  9: end

It can also be written in this way.

  1: x=1+4*rand(1,5);
  2: y=2+2*rand(1,5);
  3: plot(x,y,‘o-‘)
  4: for i=1:length(x)
  5: str1 =strcat(strcat(‘(‘,num2str(x(i))),‘,‘);
  6: str2 = strcat(num2str(y(i)),‘)‘);
  7: text(x(i),y(i),strcat(str1,str2))
8: % add text at (x, y). The text content is (x, y)
9: % x and y at each point use num2str to convert the number into a string.
 10: end

The first one is better, because he knows that the string's multiple connection methods (text (X, Y, s) are displaying the string s at the point (x, y ).

Ii. String connection

  • Enclose str1 and str2 in brackets like Matrix Elements
  1: >> str1=‘ILoveYou‘;
  2: >> str2=‘Matlab‘;
  3: >> [str1,str2]
  4: ans =
  5: ILoveYouMatlab

To verify that str1 and str2 are actually connected, call the length function to test the length of the generated string.

  • Connect with strcat Function
  1: >> strcat(str1,str2)
  2: ans =
  3: ILoveYouMatlab

Or use the following method.

  1: strcat({‘Red‘,‘Yellow‘},{‘Green‘,‘Blue‘})
  2: ans = 
  3:     ‘RedGreen‘    ‘YellowBlue‘
  4: >> {‘Red‘,‘Yellow‘}
  5: ans = 
  6:     ‘Red‘    ‘Yellow‘

The following is different.

  1: >> strcat([‘Red‘,‘Yellow‘],[‘Green‘,‘Blue‘])
  2: ans =
  3: RedYellowGreenBlue
It can be seen that this is different from the parameter location of plot.
  • Use the sprintf Function
  1: >> number=123;
  2: >> sprintf(‘%s%d‘,‘Fuck‘,number)
  3: ans =
  4: Fuck123

Class (ANS) can be used to know that the type of fuck123 is Char.

Iii. Differences between brackets and braces

Braces are used for the cellular array. If you do not understand them, you will not be able to find any information.

4. Scatter Plot

Plot ([x1, x2], [Y1, y2]). Let's see what the parameters mean.

In addition to the scatter function, the scatter chart can also be used by stem functions.

  1: >> y=x.^2;
  2: >> stem(x,y,‘fill‘);
3:> title ('y = x ^ 2 scatter plot ');

V. Graph description

The following curves Y1 = 0.2e-0.5 xcos (4 π X) and Y2 = 2e-0.5xcos (π X) are drawn with different line types and colors in the same coordinate, respectively, marking the intersection of the two curves.

1: Figure (1); % the parameter must be an integer scalar, so set must be used to set the title bar "complex functions"
2: % pay attention to the difference between observation and titile
3: Set (GCF, 'name', 'complex functions ');
4: Title ('complex functions ');
5: % if axis is used to initialize the coordinate range, the system will prompt that X is not initialized. Therefore, initialize x using other methods. axis must be placed after plot.
6: % and if the image generated by axis may be up or down, it is not often used to set the coordinate range, just set X.
  7: x=linspace(0,2*pi,1000);%xlim([0,2*pi])
8: xlabel ('x axis (m )');
9: ylabel ('y axis (m )');
 10: y1=0.2*exp(-0.5*x).*cos(4*pi*x);
 11: y2=2*exp(-0.5*x).*cos(pi*x);
 12: k=find(abs(y1-y2)<1e-2);   
13: % search for the subscript of Y1 and Y2 equal points (approximately equal)
14: X1 = x (k); % returns the X coordinate of the points equal to Y1 and Y2.
 15: y3=0.2*exp(-0.5*x1).*cos(4*pi*x1);   
16: % calculate the Y coordinate of the point where Y1 and Y2 are equal
 17:  plot(x,y1,‘b‘,x,y2,‘r‘);
 18:  hold on
 19:  scatter(x1,y3);
20: Legend ('a curve ',' B curve ', 'c curve'); % the c CURVE cannot be added because scatter and plot are not in the same system, the system prompts "undefined function or variable 'x '"
21: % plot (x, Y1, X, Y2, 'K: ', X1, Y3, 'bp'); this can also mark the vertex. If P is not added, the three lower curves are the line chart, but no P is found.
22: % It doesn't matter if you don't know. It will be better if you use an alternative solution.
23: % axis ([0 2 * pi-1 5]); % note that there are brackets in it; otherwise, always prompt "not a MATLAB expression"
 24:  grid on

The image name annotation can be used: the title ('xx Relational Graph ') and set (GCF, 'name', 'image 1') are different. Set sets the image title bar, the former is set to axis ([,]) in the image center area. --- the display range is from 0 to 22 on the X axis and from 0 to 3 on the Y axis. Xlabel ('voltage (v) '), ylabel ('current (a)') indicate the voltage (V) on the X axis, and "Current (a)" on the Y axis )"

Legend ('a curve ',' B curve ', 'c curve') is used to describe the curve in the graph, the sequence and the plot (x1, Y1, X2, Y2, X3, y3) curves 1, 2, and 3 are the same.

The question is, how to control the accuracy of the Y axis display (the X axis uses the linspace third parameter to represent a total of a few, the actual meaning of the accuracy, or X =, then the accuracy is 1 ), the Y axis cannot be like x, so I am afraid that the image is on the top and down. So I want to draw a picture first to determine the Y axis range, for example, 1 to 6. Then add y = 1:0 again. 5: 6, so you are not afraid of the deviation from the top to the bottom, and the accuracy is 0.5. To learn more, think of some simple methods. If you don't have any problems, just look for functions.

6. Draw a smooth curve

For discrete coordinate points, scatter plots, and plot line charts, the question is, how can we smooth the line chart?


Some Questions about MATLAB plotting

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: 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.