Plotting 3 vectors in Matlab GUI axes handle - matlab

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)

Related

Difference between plot and scatter matlab

Consider the following data points and plots
a = randi(50,1,200);
b = randi(50,1,200);
figure;scatter(a,b,'.')
figure;plot(a,b,'.')
When we run the following code , we receive exactly the same plots for a against b , my question is why should we even use or to rephrase again in what conditions scatter plot has advantage over plot function ? because plot seem to have more formatting options that the scatter function
plot has a concept of the order of the points mattering so you can use it to make line plots. plot also allows you to specify the input x and y values as either vectors or matrices or allows you to input multiple x and y vectors both of which allow you to plot multiple series at once:
whereas scatter only allows you to input 1 x and 1 y and they both have to be vectors. However, 'scatter' allows you to specify an area and colour vector to affect the points individually i.e.

i have 100*100 matrix, how can i make plot3 graph?

I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:
I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.
I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!

plotting 2 variable of different size in matlab

Am trying to plot 2 variable of different size length in matlab GUI using push button,
but because the variables are of different length it will not work,is there a way i can make it to plot.
d= pdist([x,y,z],'euclidean') ; % value of my distance
dd= 1:10:d; % interval and end 'd' value
FSL=-120; %value of free space loss get from the GUI
DFSL= 1:10:FSL %interval and end at FSL value
plot(dd,DFSL)
The plot code didnt work coming back with an error "
Error using plot
Vectors must be the same lengths"
You can plot vectors of two different lengths, but not against each other. You have used the syntax
plot(x,y)
which means for every element in vector x, there should be a corresponding element in vector y. In your case, you do not have this, hence the error.
You can plot like this though:
plot(x)
figure;
plot(y)
If you are looking to plot them in a single plot, subplot will be useful.

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

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.

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.