matlab x axis label set as a vector - matlab

How can I set the x axis label as a vector? For example, if I do plot(1:5), the x axis label is [1, 2, 3, 4, 5]. I'd like to set it to a vector, e.g. [1 4 5 7 10]. Note that the vector's size may be huge, so doing it manually is not acceptable.

I believe this is what you want.
y = 1:5;
x = [1 4 5 7 10];
plot(y);
set(gca,'XTickLabel',x);

you can do this by sending plot two vectors: one for x and one for y.
plot([1 4 5 7 10], 1:5);

Related

XData size error for 3D Plotting in Matlab

G =
graph with properties:
Edges: [4782×2 table]
Nodes: [692×0 table]
>> plot(G, 'XData', x1, 'YData', y1, 'ZData', z1)
Error using matlab.graphics.chart.primitive.GraphPlot
Expected XData to be an array with number of elements equal to 692.
This is the output from creating a graph from an adjacency matrix, and then trying to stick the nodes to specific coordinates (x1,y1,z1), but it gives me the error about XData being the wrong size. I followed the tutorial from here: https://www.mathworks.com/help/matlab/ref/graph.plot.html
I could reproduce the error with a smaller set:
a = [1 2 3]
b = [4 5 6]
c = [7 8 9]
d = [10 11 12]
e = [13 14 15]
f = [16 17 18]
G = graph(a,b,c)
plot(G, 'XData', d, 'YData', e, 'ZData', f)
Gives the same error except that the "number of elements equal to 6"
Looking through the documentation, it appears that you are using the G = graph(s,t,weights) form of the constructor for graph. In this form, s and t are node pairs. Each element pair [s(i),t(i)] indicates an edge between the nodes with ID s(i) and t(i). The 3rd array gives the weight for each edge. Your graph therefore has 3 edges, and 6 nodes:
a = [1 2 3];
b = [4 5 6];
c = [7 8 9];
G = graph(a,b,c)
You have nodes numbers 1 through 6, with an edge between nodes 1 and 4, one between 2 and 5, and one between 3 and 6.
Therefore, you would need also 6 coordinates for these 6 nodes in the plot function.

Plot data ignoring some X-axis values on Matlab

I'm trying to plot some data on Matlab using the following code:
x = [1 2 5 6 7 9]
y1 = [1 2 3 2 1 2]
y2 = [2 2 2 1 3 3]
y3 = [1 1 2 3 1 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
And I'm getting the following result:
Note that I have no Y values for the X positions 3, 4 and 8, but the X axis is still showing these X values in the graph.
There is some way that I can ignore the positions 3, 4 and 8 in the X-axis, and show only the Y values for the X positions 1, 2, 5, 6, 7 and 9?
I can use the following command to 'hide' these positions:
set(gca, 'XTick', x);
But the gaps related to these positions are still there.
Update:
This is the graph I'm trying to create (it was created on paint):
Note: in my case, the X-axis just represents the IDs of some images, and because of that I just need to show the numbers.
You can get the plot you want by first leaving x out in the calls to plot (so it plots against the array index) then altering the XTick and XTickLabel properties of the axes (and adjusting the x limit slightly):
plot(y1,'--.','markersize',20); hold on;
plot(y2,'--.','markersize',20);
plot(y3,'--.','markersize',20);
legend('y1','y2','y3');
xlim([1 numel(x)]); % Note numel is used here
ylim([0 4]);
set(gca, 'XTick', 1:numel(x), 'XTickLabel', cellstr(num2str(x(:))));
I am not sure if you need gaps between plot lines. If you wish no values to be plotted there, try replacing the values by nan. For example,
x = [1 2 3 4 5 6 7 8 9]
y1 = [1 2 nan nan 3 2 1 nan 2]
y2 = [2 2 nan nan 2 1 3 nan 3]
y3 = [1 1 nan nan 2 3 1 nan 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
This will give a plot like this:
Sounds like you need stem.
stem(x, y1);
legend('y1');

Matlab - Plot multiple lines using multiple points and find the length of each line

I have two matrix as
A = [1 2 3; 4 6 7; 3 6 7]
B = [2 5 6; 2 8 7; 2 8 5]
I want to plot a graph between this two matrix, I mean in such as way that A(1,1) as x coordinate and B(1,1) as Y coordinate of 1st point. Similarly, for 2nd point A(1,2) as x and B(1,2) as Y and so on. At last I should get straight line connecting this point for each row.
And then I have the measure the length of the line connecting all the points for each row, so that I can know which row have greater length
I tried this
for i=1:1:3
plot(A(i,:),B(i,:)), hold on;
end
Is it correct because I can't able to interpret and how to measure the length also??
Your way of plotting seems correct.
To calculate the length of each line I would use this code:
for i=1:1:3
len(i) = sum(sqrt(diff(A(i,:),1).^2+diff(B(i,:)).^2));
end
You don't need for loop to plot. Just do.
A = [1 2 3; 4 6 7; 3 6 7];
B= [2 5 6; 2 8 7; 2 8 5];
% Plot lines
plot(A.',B.');
% Calculate length of lines
length=sum(sqrt((diff(A,1,2).^2)+(diff(B,1,2).^2)),2);

plot vectors of data

I have three vectors of data; first column is axis x, second column axis y and third column is the measured data point value v:
x = [1 2 3 4 5];
y = [2 3 4 5 6];
v = [0 -1 +2 3 -5];
Is there a way to plot this in matlab and color the data points according to their value? I tried with scatter, but that doesn't give color coding.
Just use scatter, it can accept values for each point. Just set the size of each point, set it to be filled, turn on the colorbar and that's it. Just please don't use the jet colormap...
x = [1 2 3 4 5];
y = [2 3 4 5 6];
v = [0 -1 +2 3 -5];
pt_sz = 30;
colormap parula
scatter(x, y, pt_sz, v, 'filled');
colorbar;
grid on
You can try pcolor. It works like
z=rand(4,4);
pcolor(R,C,z);
Where z is a matrix storing the data (your v), R is the row (your x) and C is the column (your y).

Add markers to a step function in Matlab

I have the following step function:
I wish to add 2 markers, on the left hand side of the horizontal line as a filled circle and on the right hand side a circle which is not filled with color. How can I do this please? Thank you.
I used very simple code for this graph. The code is:
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
x=[1.5 2.6];
y=[2 2];
plot(x, y)
hold on
x=[2.6 5];
y=[3 3];
plot(x, y)
hold on
x=[5 6];
y=[4 4];
plot(x, y)
hold on
x=[6 6.5];
y=[5 5];
plot(x, y)
hold on
x=[6.5 8];
y=[6 6];
plot(x, y)
axis([0 9 0 7])
Do as follows for each segment (the new lines are the last two):
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
plot(x(1),y(1),'o','MarkerFaceColor','b') %// filled circle
plot(x(2),y(2),'o') %// non-filled circle