How to plot multiple Graphs with nearly similar name of ordinate arrays

조회 수: 2 (최근 30일)
Johannes Graf
Johannes Graf 2022년 6월 23일
댓글: Johannes Graf 2022년 6월 24일
Hey,
I have 60 arrays which I called Eigenform1 until Eigenform60. They all share the exact same x-Values and now i want to plot them.
And there is my Problem. I would like to use a for loop like
for i=1:60
plot(x_Values,[ Eigenvalues i ],'g-')
end
Where i in the expression i used to adress to a certain array of the 60 arrays i already defined.
Is there any solution to fix this (There surely is one I simply don't know)
Thanks and Bye

답변 (2개)

Eamon Gekakis
Eamon Gekakis 2022년 6월 23일
It is NOT recommended to dynamically name variables, here is why
That being said, this may help. In place of [ Eigenvalues i ], try this:
eval(strcat('Eigenform',num2str(ii)))
This will turn your ii iterator value into a string, combine the name string "Eigenform" with the iterator string, and then eval will evaluate the string and call the Array with the same name as the string.
Here is the documentation for these functions: eval strcat num2str
  댓글 수: 1
Johannes Graf
Johannes Graf 2022년 6월 24일
Thanks a lot and yeah i know that its not recomended to use this scheme, for several reasons. I asked more out of "I'm intrested how this may work" -reasons than out of, "Yeah this is the way to go". I only knew the way dpb i think was trying to explain to me.

댓글을 달려면 로그인하십시오.


dpb
dpb 2022년 6월 23일
"I have 60 arrays which I called Eigenform1 until Eigenform60. ... And there is my Problem. ..."
Indeed it is...
"Is there any solution to fix this..."
"Yes, Virginia, there is a Santa Claus!"
60 arrays which I called Eigenform1 until...Just "DON'T DO THAT!!!" :)
Use a cell array or a 2D array(*) where each column becomes the variable you created instead of another variable.
Specific code would depend on how you created these in the beginning; show us how that was done and we can guide to a much better and MATLAB-friendly solution.
For plotting and many other operations, the 2D or 3D array is ideal -- MATLAB is vectorized to operate on 2D arrays by column for most of its builtin anaysis functions;
plot(x,y)
where x is a vector and y a 2D array or same number of rows as numel(x) will plot each column versus x automagically; no other intervention needed. While 60 variables on one plot may be too many, you can create subset indexing expressions to select those wanted also dynamically.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by