Borrar filtros
Borrar filtros

How to use a char variable name as a numeric variable

11 visualizaciones (últimos 30 días)
Jose Andrés
Jose Andrés el 26 de Abr. de 2015
Editada: Stephen23 el 19 de Jun. de 2019
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!

Respuesta aceptada

Mohammad Abouali
Mohammad Abouali el 26 de Abr. de 2015
Editada: Mohammad Abouali el 26 de Abr. de 2015
Similar question has been asked.
Try the answers on this post:
Generally the approach that you have used is not recommended. Although pretty much on that post the question is answered by pretty much clearing the question. But this is one of those cases that I think everyone agrees that it is ok.
So, if you read that post people have suggested other methods, that does not end up to have different variable names. There are much easier way to handle multiple images.
In your case that you are dealing with multiple images, it is not bad idea to check on imageSet(), especially if you have later versions of MATLAB.
by the way, MATLAB has montage() commands which pretty much concatenates multiple images. Don't rewrite your own image concatenation unless for some other reason you need to.

Más respuestas (2)

Stephen23
Stephen23 el 26 de Abr. de 2015
Editada: Stephen23 el 19 de Jun. de 2019
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
Jose Andrés el 27 de Abr. de 2015
Thank you both of you. Finally I did with a simple loop and it works perfectly.
Thank you so much.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by