Plotting Matrix in Matlab Loop - Mathias

In summary, the conversation is about a matrix A with 10 columns and 6 rows. The person wants to plot each column of A separately using a loop and also change the color of each plot.
  • #1
mathia
15
0
Hi,
I have a matrix for instance as follow:
A=[0.087 11.43 17.14 22.86 28.57 34.29 40.00 45.72 51.43 57.15
0.174 5.671 8.506 11.34 14.17 17.01 19.84 22.68 25.52 28.35
0.261 3.732 5.598 7.464 9.330 11.19 13.06 14.92 16.79 18.66
0.349 2.747 4.121 5.494 6.868 8.242 9.616 10.98 12.36 13.73
0.436 2.144 3.216 4.289 5.361 6.433 7.505 8.578 9.650 10.72
0.523 1.732 2.598 3.464 4.330 5.196 6.062 6.928 7.794 8.660];

I want to have all of this plot:
hold all
plot(A(:,1),A(:,2))
plot(A(:,1),A(:,3))
plot(A(:,1),A(:,4))
plot(A(:,1),A(:,5))
plot(A(:,1),A(:,6))
plot(A(:,1),A(:,7))
plot(A(:,1),A(:,8))
plot(A(:,1),A(:,9))
plot(A(:,1),A(:,10))

Can I have all of those plot just by using one plot command in a loop? If this is the case how can I do?
Thank you.
Best Regards,
Mathias
 
Physics news on Phys.org
  • #2
are they separate plots?? if not overlaying them is accomplished by

Code:
for i = 2:10

plot(A(;,1),A(:,i));
hold on;

end
hold off;
 
  • #3
thank you.
yes, they are separate plots.

can I change the color of each plot by using those commands?
for example I want plot(A(:,1),A(:,2)) be read, plot(A(:,1),A(:,3)) be green and so on.
 

Related to Plotting Matrix in Matlab Loop - Mathias

1. How do I plot a matrix in a loop in Matlab?

To plot a matrix in a loop in Matlab, you can use the "for" or "while" loop structure. Within the loop, you can use the "plot" function to plot each element of the matrix. Make sure to use the "hold on" command to plot all the elements on the same figure.

2. Can I use a different color for each line in the plot?

Yes, you can use the "color" parameter in the "plot" function to specify a different color for each line in the plot. For example, if you have a matrix A and want to plot each column with a different color, you can use the code "plot(A,'color','b')", where 'b' can be replaced with any color code.

3. How can I add a legend to the plot?

To add a legend to the plot, you can use the "legend" function. You can specify the labels for each line in the plot as the input to the function. For example, if you have plotted three lines and want to label them as "Line 1", "Line 2", and "Line 3", you can use the code "legend('Line 1', 'Line 2', 'Line 3')".

4. Is it possible to save the plot as an image file?

Yes, you can save the plot as an image file in Matlab by using the "saveas" function. You can specify the file format (e.g. png, jpg) and the file name as the input to the function. For example, if you want to save the plot as a png file named "plot.png", you can use the code "saveas(gcf,'plot.png','png')".

5. Can I plot a matrix with non-numerical values?

Yes, you can plot a matrix with non-numerical values in Matlab. However, you will need to convert the non-numerical values into numerical values using functions such as "double" or "str2double". Alternatively, you can use the "scatter" function to plot non-numerical values as markers on a plot.

Back
Top