Plotting multiple vectors from a function

17 visualizzazioni (ultimi 30 giorni)
Hello everyone. I have a function which returns 'n' sets of 3 values 'Ax', 'Ay' and 'Az'. I want to plot all the 'n' sets for 'Ax', 'Ay' and 'Az' on the same axis. How can it be done?
  3 Commenti
Stephen23
Stephen23 il 6 Giu 2018
"I have a function which returns 'n' sets of 3 values 'Ax', 'Ay' and 'Az'."
This is not clear. Do you have a function with three scalar outputs, and you call this function n times, or do you call the function once and it returns three outputs each with n values, or something else?
" I want to plot all the 'n' sets for 'Ax', 'Ay' and 'Az' on the same axis."
What kind of plot: as a line plot, a scatter plot, a bar plot, ... ? Do the Ax, Ay, and Az indicate dimensions to plot?
RACHNA DANDWANI
RACHNA DANDWANI il 12 Giu 2018
Yes, I have a function with three scalar outputs and it is called n times. I want a line plot. Ax, Ay and Az are the values returned by the function.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 12 Giu 2018
Simpler than the accepted answer is to use a loop:
for k = 1:N
[Ax,Ay,Az] = yourFunction(...);
plot3(Ax,Ay,Az)
hold on
end

Più risposte (1)

Prajit T R
Prajit T R il 11 Giu 2018
Hi Rachna
As Rik mentioned, you need to use plot3 when you have three variables 'x', 'y' and 'z'. I presume that you have many sets of three values like 'Ax','Ay','Az' as one set, 'Bx','By','Bz' as the second set and so on for 'n' sets.
Let us say the size of each is 1x10. Now you wish to plot each of these 'n' sets with the corresponding 3 values each time in the same plot. You can do that as follows:
plot3(Ax,Ay,Az);
hold on
plot3(Bx,By,Bz);
.
.
.
plot3(Zx,Zy,Zz);
This will give you the plots of all 'n' sets (here, n=26 for example) in the same graph.
Hope this helps.
Prajit
  3 Commenti
Stephen23
Stephen23 il 12 Giu 2018
Modificato: Stephen23 il 12 Giu 2018
"but what if value of 'n' is very large? I can't go on using hold on so many times."
You are right, that would be a crazy way to write code. But you accepted this answer, which means that you are happy that it does exactly what you want. If you wanted to write simpler code though, you could use a loop, like my answer shows.
" How can I plot graphs such that horizontal axis is the same but vertical axis takes 3 different parameters?"
Standard MATLAB allows two y-axes: one of the left, one on the right. For multiple Y-axes you can find some submissions on MATLAB File Exchange:

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by