Plot selected variables according to flag
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 5 de Mayo de 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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!