Matlab : Plot each line of a matrix as a function of its index - matlab

I would like to create a plot with several lines, each corresponding to a row at a given matrix.
To be more elaborate, I have got a matrix M where each row represents a value that is changing along the columns. I would like to plot this change as a function of the column index to each of the rows, so to plot (e.g) the first rowI should :
plot(M(1,:));
The thing is, I would like to plot all the rows. Of course I could iterate over them, hold and plot the current one:
(plot(M(i,:))
but I'm wondering if there's a simple command or a single-liner that would do it.
I have tried plotmatrix but without much success regarding the desirable results.
Thanks!

Have you tried plot(M')?
From the first paragraph of the documentation of plot:
plot(Y) plots the columns of Y versus the index of each value when Y is a real number.

Related

plotting structure elements and dimensions in matlab

I'm trying to create a 3D plot in Matlab of a structure element, Bstruct.scen_1. In this structure, each row is a year, each column is a distance, and the cell value is a population size (so for example, row 3, column 7 would yield the number of adults in year 3 at 7 km.) I want the X-axis to be the number of columns in Bstruct.scen_1, the Y-axis to be the actual value in the cell at (X,Z), and the Z-axis to be the number of rows in Bstruct.scen_1.
Conceptually, what I'd like to accomplish is:
plot3(Bstruct.scen_1(1:num_cols), Bstruct.scen_1(cellvalue), Bstruct.scen_1(1:num_rows))
I'm struggling with the syntax of structures and would really appreciate help in plotting both the elements and the dimensions of this structure. (I mostly code in R with 'tidy' data.) Thank you!
The value of a structure field can be any data type. It sounds like the scen_1 field contains a 2D matrix. The plot3 function expects an X, Y, and Z coordinate for each data point. In your case, if you're wanting to plot the value of the matrix at each 2D location, using the function surf (or mesh) might provide a good start:
% random data for demonstration
Bstruct.scen_1 = rand(20, 10);
figure;
surf(Bstruct.scen_1);

Plotting 3 vectors in Matlab GUI axes handle

I am trying to plot 3 vectors onto matlab GUI in a serial object's callback.
I want to plot this on axes handle but the problem is it only plot last vector;
plot(handles.axes1,sensor1,'r');
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
I searched on internet and find that this issue can be solved with hold on and hold of feature so I tried this
plot(handles.axes1,sensor1,'r');
hold on ;
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
hold off;
but in this case a new figure is opened(dont know why) and again only the last plot is drawn.
I am stucked. If any one have idea of what would be the issue?
Thanks
I'm not sure why your first try using "hold" didn't work. Seems like it should have.
But in any case, you can get the desired behavior in a single command:
plot(handles.axes1,length(sensor1),sensor1,'r',...
length(sensor2),sensor2,'b',...
length(sensor3),sensor3,'g');
This specifies both an X = length(sensor_) and a Y = sensor_ to the plot command. When you only give plot a Y input, it assumes an X of length(Y). But you can't combine multiple traces in a single plot command by giving only the Y input for each, because it will try to treat the inputs as X,Y pairs.
As the vectors are the same length we can simply combine them as the columns of a matrix and then plot the matrix
plot(handles.axes1,[sensor1',sensor2',sensor3'])
However these will have the default colour order. Without specifying x values setting colors within the plot command is tricky. However (luckily) the default order starts:
blue,green,red...
so swapping the column order will plot the lines with the colours requested
plot(handles.axes1,[sensor2',sensor3',sensor1'])
(this assumes the vectors are rows, if they are columns don't transpose them)

How to plot a 2-demension graph using a n*2 matrix in matlab?

Now I get a matrix named Test with 1000 rows and 2 columns, which contains the (x, y) coordinates of 1000 dots. The first column is the values of X-coordinate, and the second is the values of Y-coordinate.
Now I want to display(plot) all the dots, but I found it's wrong if I just used plot(Test).
Could you guys give me a solution?
It sounds like you want a scatter plot, which you can get by,
plot(Test(:,1), Test(:,2), '*')
You can change the marker to a number of different symbols. For example, 'o' gives a circle, '*' gives an asterisk, '.' gives a point, etc. See the documents for plot for the full list.
If you leave off the marker specification, it will default to no marker with a solid line connecting the points -- not good for a scatter plot!
When you use the single-argument form of plot (i.e. plot(Y)), when Y is a matrix each column of Y is plotted against the row number.
If column 1 represents x and column 2 is y you need to use the two-argument form:
plot(Test(:,1), Test(:,2))

MATLAB: plotting multiple columns of a matrix

Inside a MATLAB function I have built a matrix A, whose dimensions M and N are set as parameters of the function. I would like to plot all the columns of this matrix, given a vector of indices B with length M. Hence, I use these lines:
figure
plot(B,A)
I specified figure as the MATLAB function returns more different plots.
My problem is that the program plots just two columns of the matrix with different colours (blue and violet). Where is my mistake?
Thank you for your attention.
go for
plot(repmat(B,1,N),A);
or
plot(repmat(B,N,1),A);
(depending on your rows/columns). You need to have same size matrices in plot.
Moreover, if B are just consecutive indexes, you may want to consider Plot(A) (or Plot(A')).
I noticed that there was an error which caused the overlap of the different curves, so the way which I used to plot the colums of a matrix is valid. However, the method proposed by Acorbe is a possibility, too.

Making a 3D plot of multiple column vectors

I have multiple vectors of varying lengths that I would like to plot next to each other in 3D space in Matlab.
As an example:
Say I have three vectors:
X is a 5x2 vector,
Y is a 10x2 vector and
Z is a 15x2 vector.
Each element of every vector has the format:
x value, y value
but the x values of the various vectors do not match.
I would like to plot these vectors in 3D space, next to each other. The reason why I don't want to plot them using "hold" is because most of the data have the same values, but I would like to see how many of the plots have the same value at a specific time.
I hope my questions makes sense. Please just ask if anyone is unsure.
I think you are looking for the function ribbon.
Documentation: http://www.mathworks.fr/help/techdoc/ref/ribbon.html
EDIT:
if your x's do not have the same length, you can combine it with interp1 as follow:
x1=0:0.1:1;
x2=0:0.02:1.5;
y1=x1.^2;
y2=sqrt(x2);
y2=interp1(x2,y2,x1);
ribbon(x1',[y1;y2]')