How to define variable in cell array and plot with for loop
Ältere Kommentare anzeigen
Hi everyone:
I have some output data, defined by different variable names. I am trying to plot the data using a for loop instead of using the plot function each time for the different variables. I have put the variables in a cell array and tried to plo using the following code:
figure
prop={'maxTwrBsMyt_S','maxTwrBsFxt_S','meanTwrBsMyt_S', 'meanTwrBsFxt_S'};
for ii = 1:numel(length(prop))
plot(A, prop{ii});
end
However, I get the following error.
Error using plot
Invalid color, marker, or line style.
Can you please help me to solve this problem.
Thank you.
Regards,
AOAW
8 Kommentare
KSSV
am 30 Mai 2022
What are the variables in cell array? I mean what is this 'maxTwrBsMyt_S'?
If it is a variable name, then you cannot do that. What exactly you want to do?
Andre White
am 30 Mai 2022
Andre White
am 30 Mai 2022
"Is something wrong with how I have defined the varibales?"
Yes: the arrays should be defined as elements of cell** array, then you can trivially loop over them using basic indexing.
In contrast your approach requires dynamically accessing the variable names, which forces you into writing slow, complex, inefficient, obfuscated code which is difficult to debug. Poor data design forces you into writing slow, complex code.
If you want to loop over something, then use indexing. It really is that simple.
You did not tell us the most important information: how did you get all of those variables into the workspace? I doubt that you sat and wrote them all out by hand, so you must have created/imported them somehow: that could be a suitable place to fix this poor data design.
** or some other array type, e.g. ND numeric, structure array, table, etc.
Andre White
am 30 Mai 2022
" I am sorry but I have no idea what you mean by poor data design or that I was not looping over an idex. If you give an example of the wrong approach and then the correct approach"
Consider multiple arrays named using meta-data (e.g. pseudo-indices), e.g.:
A1 = [..];
A2 = [..];
A3 = [..];
..
An = [..];
Accessing this data will be slow, complex, inefficient, buggy, and difficult to debug. Trying to process such meta-data will be complex, fragile, and difficult to work with:
Much better data design uses one array (just like MATLAB is designed for), which can be trivially accessed using indexing, e.g.:
C{1} = [..];
C{2} = [..];
C[3} = [..];
..
C{n} = [..];
In your case it is clear that you included some kind of meta-data into the variable names, thus making your data difficult to work with. Understand that meta-data is data, and data should be stored in a variable (not in variable names), then you can write much more efficient, robust, generalizable code. For example, your data arrays can easily be stored together with any meta-data in one structure array, e.g.:
S(1).type = 'max';
S(1).name = 'TwrBs';
S(1).what = 'Myt_S';
S(1).data = [..];
S(2).type = 'max';
S(2).name = 'TwrBs';
S(2).what = 'Fxt_S';
S(2).data = [..];
S(3).type = 'mean';
S(3).name = 'TwrBs';
S(3).what = 'Myt_S';
S(3).data = [..];
etc.
Note how that can be trivially looped over using an index, either when the structure array is created or accessed. Note how any meta-data can be included, even numeric values, text, arrays that are not valid in variable names (unlike your fragile approach, which would fail for any names that include invalid characters). This is simple and efficient to access.
Andre White
am 30 Mai 2022
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

