How to use a char variable name as a numeric variable
Afficher commentaires plus anciens
Hello everyone,
I have concatenated images and generated some new variables with this loop:
% mymatrix=768x128
n=size(mymatrix,1)/size(mymatrix,2);
for k = 1:n-1
v = genvarname('IOri',who);
eval([v ' =mymatrix((128*k):(128*k+127),:)']);
...
% And here I need to use the value of my new variable
end
I need to use the values of the variables "v" in every loop called 'IOri','IOri1','IOri2'... to execute other code lines before the loop ends.
I can't use this v variable directly because it contains a char name of the IOrik created, and I can't use this IOrik name because it changes in every loop iteration. I need to get this IOrik value assigned to the v after his creation.
Hope I have explained myself propertly.
Thank you!
Réponse acceptée
Plus de réponses (2)
Use a simple cell array loop instead:
C = cell(size(mymatrix));
n = size(mymatrix,1)/size(mymatrix,2);
for k = 1:n-1
C{k} = ... any calculations here
end
Avoid creating dynamically named variables in MATLAB
Jose Andrés
le 27 Avr 2015
0 votes
Catégories
En savoir plus sur Loops and Conditional Statements 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!