convert a line plot to a matrix - matlab

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

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?

How to solve error while doing interpolation one point with many points in MATLAB

I am trying to interpolate many points to one point in MATLAB. But I am getting error
The grid vectors do not define a grid of points that match the given values.
My code is given below
A = [2 6;3 7]
B = [3 6;4 9]
Xq=[A(:,1) [10;10]]
Yq=interp1(A,B,Xq','linear','extrap')
Actually I want to generate a line passing through one point from many points and as shown in below figure and extend upto axis of plot
'For loop' would be a solution.
One example is this.
A = [2 6;1 6;3 6]
B = [3 6;2 6;3 6]
L=size(A)
xArr=zeros(L(1), L(2));
yArr=zeros(L(1), L(2));
nLine=L(1); %number of lines
for k=1:nLine
Xq=[A(k,:) 10]
Yq=interp1(A(k,:),B(k,:),Xq','linear','extrap')
xArr(:,k)=Xq';
yArr(:,k)=Yq';
end
plot(xArr,yArr,'*-')
If you don't want to use for loop, you could directly calculate gradient and intercept of linear function.
A = [2 6;1 6;3 6]
B = [3 6;2 6;3 6]
L=size(A)
limX=10; %x limit
a=(B(:,2)-B(:,1))./(A(:,2)-A(:,1)); %gradient vector
b=B(:,1)-a.*A(:,1); % intercept vector
y=a*limX+b % linear function
Aq=[A';limX*ones(1,L(1))];
Bq=[B';y'];
figure(2)
plot(Aq,Bq,'*-')

Plot 3D histogram using bar3

I'm currently trying to plot the output of hist3 using bar3. This is a simple example:
vec_x = [1 2 4 5 7 8 9 3 8 7 2]';
vec_y = [1 3 9 5 7 8 1 3 2 9 2]';
vec_bin_edges = 0:9;
hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
mat_joint = hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
figure
bar3(mat_joint, 1);
axis tight
In order to demonstrate my issue, I made two pics of both figures:
This one is the output of hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
This one is the output of bar3(mat_joint, 1);
As you can see, the bar3 function does not really "bin" the data values as hist3 does, so the bars are shifted slightly in their positions. My question is now, whether it's possible to make the bar3 plot look exactly like the hist3 plot. My motivation to do so is, that I need to modify the mat_joint matrix and plot it again, which is not possible using hist3.
EDIT: The different colors are not important, it's just about the bin positions
ok, I figured it out:
set(gca, 'xtick', [1.5:1:10.5]);
set(gca, 'ytick', [1.5:1:10.5]);
vec_bin_labels = 1:10;
vec_string_bin_labels = reshape(cellstr(num2str(vec_bin_labels(:))), size(vec_bin_labels));
set(gca, 'xticklabel', vec_string_bin_labels);
set(gca, 'yticklabel', vec_string_bin_labels);

In MATLAB, Saving Plots to a Folder with Title Name

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.

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