How to define variable in cell array and plot with for loop

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

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?
maxTwrBsMyt_S= [5243 7308 6990 6765 8294 7451 7955 7541 8177 6860 6387 8711]
A= [1:12]
The other variables listed also contains numbers. I am trying to plot A against each variables using a loop.
Intead of typing
plot(A, variable1)
plot(A, variable 2)
etc.
Sorry about not being clear. Hope this is clear now.
Thank you.
@Stephen23 what are you trying to say here. Is something wrong with how I have defined the varibales?
Regards,
AOAW
Stephen23
Stephen23 am 30 Mai 2022
Bearbeitet: Stephen23 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.
@Stephen23 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 I believe that would help as I would like to understand.
By the way I did type the varibales by hand.
AOAW
" 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

figure
prop={maxTwrBsMyt_S, maxTwrBsFxt_S, meanTwrBsMyt_S, meanTwrBsFxt_S};
for ii = 1:numel(length(prop))
plot(A, prop{ii});
hold on
end
hold off
legend({'maxTwrBsMyt_S','maxTwrBsFxt_S','meanTwrBsMyt_S', 'meanTwrBsFxt_S'});

5 Kommentare

If each variable is of size 1x12. (Which I assume it is) ;
prop={maxTwrBsMyt_S, maxTwrBsFxt_S, meanTwrBsMyt_S, meanTwrBsFxt_S};
plot(A, cell2mat(prop));
Andre White
Andre White am 30 Mai 2022
Bearbeitet: Andre White am 30 Mai 2022
@KSSV thanks for this suggestion. However, it did not solve the problem. So I will define the problem again and you see if you could help. Suppose I have the following data:
A=[1 2 3 4 5]
B=[10 20 30 40 50]
C=[100 200 300 400 500]
D=[1000 2000 3000 4000 5000]
If I want to plot A against B, C and D I could use:
plot(A,B)
plot(A,C)
plot(A,D)
However, I have variables from B to Z and want to plot them in a loop. The variables are each separate varibales with numeric data, not cell arrays. The names were placed in cell array like so.
prop={'B','C','D'}
Then I used the following code to plot:
for ii=1:length(prop)
plot(A,prop(ii))
end
However, I got the following error:
Error using plot
Invalid color, marker, or line style.
This is because Matlab does not seem to recognize the names B,C and D when they are used in a cell array.
The core of the issue is how to plot the data that I have using a for loop. Hope this is clearer now and you could help to solve it.
Thank you.
A=[1 2 3 4 5] ;
B=[10 20 30 40 50] ;
C=[100 200 300 400 500] ;
D=[1000 2000 3000 4000 5000] ;
P = {B',C',D'} ;
P = cell2mat(P) ;
plot(A,P)
legend('B','C','D')
You have to put the content of the variables into the cell array, instead of the name of the variable.
You can combine plotting and labeling by using a function:
A=[1 2 3 4 5]
A = 1×5
1 2 3 4 5
B=[10 20 30 40 50]
B = 1×5
10 20 30 40 50
C=[100 200 300 400 500]
C = 1×5
100 200 300 400 500
D=[1000 2000 3000 4000 5000]
D = 1×5
1000 2000 3000 4000 5000
plotvars(A, B, C, D)
function plotvars(x, varargin)
for ii = 1 : length(varargin)
plot(x, varargin{ii}, 'DisplayName', inputname(ii+1));
hold on
end
hold off
legend show
end
Andre White
Andre White am 30 Mai 2022
Bearbeitet: Andre White am 30 Mai 2022
@Walter Roberson and @KSSV thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by