Plotting multiple vectors from a function

17 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-6-6
"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 2018-6-12
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.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-6-12
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

更多回答(1 个)

Prajit T R
Prajit T R 2018-6-11
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 个评论
Stephen23
Stephen23 2018-6-12
编辑:Stephen23 2018-6-12
"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:

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by