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

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 30 May 2022
Edited: Stephen23 on 30 May 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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 May 2022
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 Comments
Walter Roberson
Walter Roberson on 30 May 2022
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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!