Draw a portion of a line in Matlab - matlab

I just wanna draw a portion of a line. I'll try to be more specific.
Now I wanna see just a portion of the red line..but I don't know how to do it in Matlab. I wanna show like a little portion around the red dot.

Use plot command:
x = [0 3];
y = [1 -4];
plot(x,y,'r');
In order to draw 2 shapes together, use hold on:
x = -2*pi:0.01:2*pi;
y = sin(x);
plot(x,y,'k');
hold on;
x = [0 3];
y = [1 -4];
plot(x,y,'r');

use line to draw what you need. Say you want to draw a point going from (0,1) to (3,-4), type:
line ([0 3], [1 -4])

Related

Vertical lines with text in Matlab plot

I have created a plot in Matlab. Let's assume for simplicity that I have the following plot:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Now I would like to add vertical lines (going from the bottom of the figure to the top) at positions x = 1, x = 3 and x = 5. Additionally, the vertical lines should have text (next to the line or on top of the line). For example, for the line at x = 1 I would like to have the text "test 1".
How can this be done? This seems to be a pretty tricky thing in Matlab.
Here are some ways to draw lines:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y,[1 1],[-1 1],[3 3],[-1 1],[5 5],[-1 1]);
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y); hold on;
for ind1 = 1:2:5
line([ind1 ind1],[min(y) max(y)],'Color',[0 0 0]);
end
x = 0:pi/100:2*pi;
y = sin(x);
A = zeros(6); A(sub2ind(size(A),1:6,[2 1 4 3 6 5])) = 1;
plot(x,y); hold on; gplot(A, [repelem(1:2:5,1,2).', reshape(repelem([1 -1],3,1).',[],1)]);
Etc.
Either use hold on and plot in several commands, or provide all inputs to your plot function right aways to get the desired result. Consult the documentation of the above functions for more information.
For texts refer to text.
for i=1:2:5
hold on
plot([i i],[0 1])
s=sprintf('test%1.0f', i)
t=text(i,1,s)
set(t,'Rotation',90)
end

Fill area between a polyline and a horizontal line in Matlab/Octave

I'm trying to fill the areas between a polyline and a horizontal line (that cut this polyline in several points) using Octave/Matlab, but I want to keep only the areas below the horizontal line.
This is what I've come up so far:
x = 0:0.5:6;
y = [3 2.5 1 1 1 2.5 3 2.5 1 1 1 2.5 3];
yline(1:13) = 2;
figure(1)
plot(x,y,'k')
fill([x fliplr(x)],[y yline],'g')
axis equal
xlim([-1 7]);
I searched for days to find a solution but I only went close to the answer here, here and here (unfortunately this last one is only for r-code).
You can use the following trick:
Fill normally, as you do in your code. No need for coloring the edges; it will be done later.
Draw a white rectangular patch to cover the part you don't want filled. No edge color here either.
Plot the lines on top of that.
Code:
x = 0:0.5:6;
y = [3 2.5 1 1 1 2.5 3 2.5 1 1 1 2.5 3];
yline(1:13) = 2;
figure(1)
fill([x fliplr(x)],[y yline],'g', 'edgecolor', 'none')
hold on
patch([min(x) max(x) max(x) min(x)],[yline(1) yline(1) max(y) max(y)], 'w', ...
'edgecolor', 'none')
plot(x,y,'k')
plot(x,yline,'k')
axis equal
xlim([-1 7]);
Resulting figure:

Plotting array of x and y values as points

I can't understand this: when I write the following code I obtain this graph in matlab.
x = [0 1 2 3 4 5];
y = [0 1 0 1 0 1];
figure
plot(x,y);
I had just expected that only the points written in arrays x and y would be plotted ,but the graph shows lines also...
I can't understand why is it so... Please help where am I wrong
Try to use the following
figure(10);
plot(x,y, '.');
figure(20);
plot(x,y, 'x');
figure(30);
plot(x,y, '-r');
See the differences... a dot-scatter, x-scatter and red line plot.
In the plot documentation you can read more about line styles. By default it is a blue line, as you can see in your plot!

Crossed stem plot in Matlab

I would like to reproduce in Matlab a plot that looks like this:
The stem3 plot command sounds nice but only for the vertical stems. Not the second series with the horizontal ones.
Everything would be easy if I could plot using the usual commands and rotate the result.
How a about this? Manually plot each line in 3D stemming from the x axis:
x = 0:.01:2*pi*3;
z = sin(x);
y = -sin(x);
hold on
for n = 1:numel(x);
plot3([x(n) x(n)], [0 y(n)], [0 0], 'r');
plot3([x(n) x(n)], [0 0], [0 z(n)], 'b');
end
view(15,25)
As noted by #TheMinion, it's easier to use fill3:
x = 0:.01:2*pi*3;
z = sin(x);
y = -sin(x);
fill3(x,y,zeros(size(x)),'r')
hold on
fill3(x,zeros(size(x)),z,'b')
view(15,25)

MatLab--How would I generate an n-sided shape wher n >= 4

I'm new to Matlab but I know a bit about programming.
For class, we have been asked to generate a matrix that gives the vertices of a two dimensional n-sided shape where n>=4. Then, generate the vectors to connect the vertices. We were also given a hint: a vector for each segment can be found by adding the vectors drawn from the origin to each of two adjacent vertices.
I know how to create a matrix using A = [1 1; 1 2; 2 2; 2 1] but I'm not sure how to draw the vectors given this or any other matrix.
The plot() function looks promising, but I'm unsure how to use it with the matrix.
Thank you for any suggestions.
Btw, I'm using Matlab 2011a
I'm not exactly sure how your matrix represents your shape but you might for example let the x-coordinates of the shape be the first column of your array, then let the y-coordinates be the 2nd column, like:
A = [1 1; 1 2; 2 2; 2 1];
x = A(:,1);
y = A(:,2);
fill(x,y,'g');
axis([0 3 0 3]);
axis square;
Which in your case plots a square from the matrix A:
Or construct something a little more complicated like a pentagon:
theta = [0:pi/2.5:2*pi];
x = sin(theta);
y = cos(theta);
% your matrix is then:
B(:,1) = x;
B(:,2) = y;
B
figure;fill(x,y,'g');
axis square;
Which gives:
If you just want to plot the outline with plot (not fill the interior with fill), just remember you have to repeat the initial point at the end so that the polygonal line is closed:
A = [1 1; 1 2; 2 2; 2 1];
B = [A; A(1,:) ]; %// repeat first row at the end
plot(B(:,1),B(:,2))
axis equal %// same scale on both axes
axis([min(x)-.5 max(x)+.5 min(y)-.5 max(y)+.5]) %// larger axes for better display