XData size error for 3D Plotting in Matlab - 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.

Related

Matlab bar plot in specific time points of each measured values

I want to plot vertical lines corresponding to values obtained at specific time points.
Example:
a = [0 5 7 9 ] at 0 seconds,
b = [0.5 6 6.5 11] at 2 seconds,
c = [0 4 2 10] at 4 seconds
Each time point will be a vertical line between the maximum and minimum of the vectors. I also need to mark the start and end points of a, b and c, for instance a should have a circle (or star etc.) at 0 and 9.
Here is an example output:
You can use line with end markers.
% Your data
a = [0 5 7 9 ];
b = [0.5 6 6.5 11];
c = [0 4 2 10];
% Combine to get min/max values
data = [a; b; c].';
mins = min(data);
maxs = max(data);
% Plot using line, nice flexible method which plots vertical lines at points 2:2:n
line(repmat(2*(0:numel(mins)-1), 2, 1), [mins; maxs], 'color', 'k', 'marker', 'o')
Output:
If you want different markers on each end, or different colours, please see this answer which gives more detailed examples.

MATLAB: Forming an array of matrices and do operations with respect to each matrix "element"

I have an array of matrix m such that
m1 = [1 2;3 4];
m2 = [2 7; 8 9];
m3 = [9 7; 8 91];
m = [m1 m2 m3]
m =
1 2 2 7 9 7
3 4 8 9 8 91
I also have a vector
v = [1 2 3];
such that i want the operations between v and m result in h such that
h = [1*m1 2*m2 3*m3] = [h1 h2 h3];
I imagine I have to do this in 3-dimensional array for h, which is a 3d array. Or maybe there are better ways.
Let A be a simple 2 by 2 matrix, rand(2,2).
From h i want to extract h1 h2 and h3 out(or better not doing any extraction) and perform operations to A such that
1.
h1*A*h1'
h2*A*h2'
h3*A*h3'
and
2.
h1*h1', h2*h2', h3*h3'.
Why i want to do this in array is because i have a lot of matrix mi so I want to avoid for loop by vectorization.
From what i could understand from the question, i suppose element-wise multiplication would be better choice. You would just have to use proper concatenation and repeatation of matrices.
m1 = [1 2; 3 4];
m2 = [2 7; 8 9];
m3 = [9 7; 8 91];
% Concatenate to create 3D matrix
m = cat(3,m1,m2,m3);
v = [1 2 3];
% Create similar 3D matrix
v1 = cat(3,ones(size(m1))*v(1),ones(size(m2))*v(2),ones(size(m3))*v(3));
% Simple element wise multiplication
h = m.*v1;
% Creating a repeated 3D matrix A, repetation is along third dimension
A = repmat(rand(2,2),[1 1 3]);
% Outputs
op_1 = h.*A.*permute(h,[2 1 3]);
op_2 = h.*permute(h,[2 1 3]);

Merging 4 separate subplots in 1 figure having 4 subplots

I have 4 different figures. Each figure contains 2 subplots (2 rows and 1 column)
The figures can be generated using the following code.
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
for i = 1 : 4
figure(i)
subplot(2,1,1)
bar(y)
subplot(2,1,2)
bar(y)
end
Having these 4 figures, is it possible to combine them in 1 figure?
the solution provided does not work with this other example where I create the figure using barwitherr..why?
for i = 1 : 4
figure(i)
subplot(2,1,1)
barwitherr([1 2 3 4;1 2 1 2], [5 6 7 8;1 2 3 4])
subplot(2,1,2)
barwitherr([1 2 3 4;1 2 1 2], [5 6 7 8;1 2 3 4])
end
for i = 1:4
figure(i);
ax = gca;
f = get(ax, 'children');
figure(5);
s = subplot(2, 2, i);
copyobj(f, s);
end
This may not be exactly what you want but is very extensible. You can loop through each of the original 4 figures and get handles for each of the subplots within it. Once the figure we are interested in using figure(i) is the current gcf object we can get a handle to each of the subplot elements with s = subplot(2, 1, i) providing we know the structure of the subplots and i is the subplot we are interested in.
We can then we use copyobj() and allchild() to copy over each of the subplots to a new subplot in the new figure
copyobj(allchild(h), s)
allchild() copies over all of the information in barwitherr() that is left out from the code you've copied from a previous edit of my answer to your question.
If we put all this together we can produce the full code as
for i = 1:4
figure(5);
n = i + (i - 1);
s1 = subplot(4, 2, n);
s2 = subplot(4, 2, n+1);
h = figure(i);
hs1 = subplot(2, 1, 1);
hs2 = subplot(2, 1, 2);
copyobj(allchild(hs1), s1)
copyobj(allchild(hs2), s2)
end
n = i + (i - 1); is used to replicate the original ordering. The output produced by this is

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

matlab - find indices of elements in x

I have a matrix x of size Nx2 (contains (x,y) coordinates) and a matrix c of size Px1 (P<=N) that contains certain x-coordinates which I'm interested in. For example:
x = [10 3; 21 9; 98 54; 4 30; 37 12];
c = [4 98];
I want to get the coordinates of the elements in c (in the above case [4 3]). How can I do this? I've only found a way when c is a 1x1 matrix (i.e. a scalar).
ismember can be used for testing membership of multiple values. You can slice the N-by-2 matrix to search only x-coordinates.
coords = [1 2; 3 4; 5 6; 7 8];
c = [3 7 99];
[v i] = ismember(c, coords(:, 1));
i =
[2 4 0]
i should contain the indices where values in c appear as the x-coordinate in coords, or a 0 if the element is not found.
If you have a recent version of Matlab, you can replace v with ~.