Plot selected variables according to flag
Show older comments
I have three variables
x1, x2 and x3
and vector, say,
flag = [1 0 1]
If I want to plot on the same figure x1, x2 and x3, but only if flag(i) == 1, how can I do it without using if-else and switch-case?
I'm looking for a more compact way of writing:
figure; hold on;
if (flag(1))
plot(x1)
elseif (flag(2))
plot(x2)
elseif (flag(3))
plot(x3)
end
Also, how do I add a legend to the plot, that changes with the number of variables plotted?
1 Comment
Stephen23
on 5 May 2017
Creating lots of separate variables will make your code more complex than it needs to be. When you put all of your data together into one array (numeric, cell, struct, table,...) then accessing it is trivial using indexing or fieldnames.
Accepted Answer
More Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!