In MATLAB, Saving Plots to a Folder with Title Name - matlab

In MATLAB, I'm trying to plot a series of plots in a loop with the following data:
x1 = [ 1 2 3 4 5]
y1 = [ 1 1 1 1 1]
x2 = [ 1 2 3 4 5]
y2 = [ 2 2 2 2 2]
x3 = [ 1 2 3 4 5]
y3 = [ 2 2 2 2 2]
plot(x,y)
title('First Plot')
THEN suppress the output and save all plots to a folder,
with the each file displaying the title names:
First Plot
Second Plot
Third Plot

For Saving the plot to a file, with the title name, you can use the following
graphTitle='first plot';
hold on
h=figure(1);
title('first plot');
hold off
fileName=strcat('path to save',graphTitle,'.jpg');
print(h,'-djpeg',fileName);
If you need to create and save a lot of files, create a vector of file names, of the same size as the number of vectors (or dimension of the matrix) you need to plot. In a look create a handle using the index of the current file Name and do the above, you should be able to print the with the title you need etc.
When you use the above code, all the plots are visible on the screen and then printed to the file.

Related

How to plot several vectors qith quiver3 in matlab

Im trying to plot the elements of a given matrix
A = [ 1 2 3; 3 4 5, 5 6 7];
using quiver3, so all of these are in the same figure using:
figure(3);
hold on;
quiver3(0,0,0,A(1,1),A(1,2),A(1,3),0);
quiver3(0,0,0,A(2,1),A(2,2),A(2,3),0);
quiver3(0,0,0,A(3,1),A(3,2),A(3,3),0);
with the next result
instead of something like this
How can tell to quiver3 that plots all the vectors at the same time, or is there anyother command that can do this?

convert a line plot to a matrix

I connect several points two by two by
plot([x1,x2],[y1,y2]) %create line
% x1,y1 = coordinates of point 1
% x2,y2 = coordinates of point 2
and I would like to know if it's possible to keep this different links into a matrix to display them subsequently with imagesc or imshow (this matrix will also be useful to me outside the display later)
As long as I understand, you are trying to plot multiple "lines" in one plot without using hold.
You can use concatenation for this purpose as shown below:
plot([1 ;2 ],[1;2] );
xlim([1 6]);
ylim([1 6]);
plot([3 ;4 ],[3;4] )
xlim([1 6]);
ylim([1 6]);
plot([5 ;6 ],[5;6] );
xlim([1 6]);
ylim([1 6]);
Finally if you want to plot these all lines into one plot you can use concatenation in the second dimension.
plot([1 3 5 ; 2 4 6 ],[1 3 5 ; 2 4 6] );
In order to save it and make it again plot table you can store in matrix with another dimension
matrixTest(:,:,1) = [1 3 5 ; 2 4 6 ];
matrixTest(:,:,2) = [1 3 5 ; 2 4 6 ];
plot(matrixTest(:,:,1),matrixTest(:,:,2));

Count occurrences and stretch array in matlab

Let
input = [0 0 0 5 5 7 8 8];
I now want to transform this vector into the form
output = [3 3 3 3 5 5 6 8];
Which basically is a stairs plot.
Explanation
The input vector is used to plot data points along the x-axis. The y-axis is thereby provided by 1:length(input). So the resulting plot shows the cumulative number of datapoints along the y-axis and the time of occurrence along the x-axis.
I now want to fit a model against my dataset. Therefor I need a vector that provides the correct value for a certain time (x-value).
The desired output vector basically is the result of a stairs plot. I am looking for an efficient way to generate the desired vector in matlab. The result of
[x, y] = stairs(input, 1:length(input));
did not bring me any closer.
It can be done with bsfxun as follows:
x = [0 0 0 5 5 7 8 8];
y = sum(bsxfun(#le, x(:), min(x):max(x)), 1);
This counts, for each element in 1:numel(x), how many elements of x are less than or equal to that.

How can I plot filled rectangles as a backdrop for a desired target in MATLAB?

I have two datasets, one of which is a target position, and the other is the actual position. I would like to plot the target with a +/- acceptable range and then overlay with the actual. This question is only concerning the target position however.
I have unsuccessfully attempted the built in area, fill, and rectangle functions. Using code found on stackoverflow here, it is only correct in certain areas.
For example
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1; %variation in target size
y2 = y-1;
t = 1:15;
X=[t,fliplr(t)]; %create continuous x value array for plotting
Y=[y1,fliplr(y2)]; %create y values for out and then back
fill(X,Y,'b');
The figure produced looks like this:
I would prefer it to be filled within the red boxes drawn on here:
Thank you!
If you would just plot a function y against x, then you could use a stairs plot. Luckily for us, you can use the stairs function like:
[xs,ys] = stairs(x,y);
to create the vectors xs, ys which generate a stairs-plot when using the plot function. We can now use these vectors to generate the correct X and Y vectors for the fill function. Note that stairs generates column vectors, so we have to transpose them first:
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1; %variation in target size
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
X=[ts.',fliplr(ts.')]; %create continuous x value array for plotting
Y=[ys1.',fliplr(ys2.')]; %create y values for out and then back
fill(X,Y,'b');
Again, thank you hbaderts. You answered my question perfectly, however when I applied it to the large data set I needed for, I obtained this image
https://dl.dropboxusercontent.com/u/37982601/stair%20fill.png
I think it is because the fill function connects vertices to fill?
In any case, for the potential solution of another individual, combined your suggested code with the stair function and used the area function.
By plotting them on top of one another and setting the color of the lower area to be white, it appears as the rectangular figures I was after.
%sample code. produces image similar to o.p.
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1];
y1 = y+1;
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
area(ts,ys1,'FaceColor','b','EdgeColor','none')
hold on
area(ts,ys2,'FaceColor','w','EdgeColor','none')
https://dl.dropboxusercontent.com/u/37982601/stair%20area.png
Thanks again for your help and for pointing me in the right direction!

Need help in plotting lines between points

I need help in plotting lines between points.
Suppose, I start with creating 6 random points-
x = rand(6,1);
y = rand(6,1);
So my points are (x(1),y(1)), (x(2),y(2)), (x(3),y(3)), (x(4),y(4)), (x(5),y(5)), (x(6),y(6))
Now I want to draw straight lines between the points 1 & 5, 2 & 6, 3 & 4
and plot them in a single diagram. So I get 3 straight lines.
Any help would be highly appreciated.
You can do this with one call to PLOT. If you reshape your x and y data into matrices with each column containing a set of coordinates for one line, then PLOT will draw a different colored line for each column:
index = [1 2 3; 5 6 4]; %# The index to reshape x and y into 2-by-3 matrices
plot(x(index),y(index)); %# Plot the lines
Here are two ways to do this:
First way, using hold on. These lines are separate, i.e if you turn one red, the others will stay blue.
%# plot the first line
plot([x(1);x(5)],[y(1);y(5)]);
hold on %# this will prevent the previous plot from disappearing
%# plot the rest
plot([x(2);x(6)],[y(2);y(6)]);
plot([x(3);x(4)],[y(3);y(4)]);
Second way, making use of the fact that NaN does not get plotted. These lines are grouped, i.e. if you turn one red, all will be red.
%# create array for plotting
xy = NaN(8,2);
%# fill in data
xy([1 2 4 5 7 8],1) = x([1 5 2 6 3 4]);
xy([1 2 4 5 7 8],2) = y([1 5 2 6 3 4]);
%# plot
plot(xy(:,1),xy(:,2))