How to define variable in cell array and plot with for loop
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-5-30
编辑:Stephen23
2022-5-30
" 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.
采纳的回答
Walter Roberson
2022-5-30
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 个评论
Walter Roberson
2022-5-30
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]
B=[10 20 30 40 50]
C=[100 200 300 400 500]
D=[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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!