Generic Variable Names and Cell Arrays
Afficher commentaires plus anciens
Hello, need to plot different plots, with data delivered in vectors named like:
y_ea, y_eb, c_ea, c_eb, ...
I store the two components of those names before execution in two cellarrays:
response('y','c','k','w')
impulse('ea','eb','em','ew')
thus there are 16 combinations in this case, each one a vector I want to plot. My current solution is using eval but I often read not to use eval but use cellarrays instead. So how do I tackle my problem with cellarrays? Here my current solution with eval:
for r=1:length(response)
for i=1:length(impulse)
str_plot_name = [response{r} '_' impulse{i}]
eval(['plot(' str_plot_name ');'])
end
end
thx for any help.
Réponse acceptée
Plus de réponses (1)
You should avoid organizing your data by using 16 different variables. Instead, you can use a 3D matrix M(r,i,:) where r runs from 1 to 4, corresponding to your responses 'y', 'c', 'k' and 'w' and i runs from 1 to 4, corresponding to your impulses 'ea', 'eb', 'em', 'ew'. Then you can use in your two for loops
plot(M(r,i,:))
1 commentaire
Image Analyst
le 29 Nov 2014
Sven's comment to Thorsten moved here, instead of being an Answer:
The data comes from another script, thus I can't change how its organized. Would have liked more 3D Matrix-form, but have to handle it this way. So I would still need an answer here.
But I didn't know the one line loop form with a matrix so thx for that already.
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!