# Plotting multiple vectors from a function

43 views (last 30 days)
RACHNA DANDWANI on 6 Jun 2018
Commented: RACHNA DANDWANI on 13 Jun 2018
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?

Rik on 6 Jun 2018
What do you mean? Is plot3 something you're looking for?
Stephen Cobeldick on 6 Jun 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 on 12 Jun 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.

Stephen Cobeldick on 12 Jun 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

Prajit T R on 11 Jun 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

RACHNA DANDWANI on 12 Jun 2018
Thank you very much, but what if value of 'n' is very large? I can't go on using hold on so many times. Besides that, I don't wish to plot them all in 3-D. I want to plot them like how different functions can be plotted in the same graph, like for same horizontal axis, I want to have values of Ax as one parameter on the vertcial axis, values of Ay as second and values of Az are third parameter. How can I plot graphs such that horizontal axis is the same but vertical axis takes 3 different parameters?
Thank you
Stephen Cobeldick on 12 Jun 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:
RACHNA DANDWANI on 13 Jun 2018
Thank you very much