Could I use a dynamic character or string in a for loop to call a variable with different name?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Yuki Himekawa
el 25 de Ag. de 2014
I couldn't figure out the best way to ask this question so apology for the confusing title. Let's say I had multiple variables with different name called var1,var2,var3... with values in each.
Is there a way to access each of the variable in such a way like
for k = 1:10
variable(k) = (variable%d,k)
end
So that I can access the variables with different names where %d can take on given integer similar to C language? Another way might be if there was a variable called GreenEggs&Ham and I want to access it like
A = 'GreenEggs'
B = 'Ham'
BreakFast = (%s&%s,A,B);
Respuesta aceptada
Andrew Reibold
el 25 de Ag. de 2014
Editada: Andrew Reibold
el 25 de Ag. de 2014
Use eval , Its really easy! Reads a string as a variable.
Here is an example. Using the base 'var', I increment the variable number and get the data from each one.
var1 = 10
var2 = 20
var3 = 50
for i = 1:3
variables(i) = eval(['var',num2str(i)])
end
3 comentarios
José-Luis
el 25 de Ag. de 2014
Using eval is generally a bad idea and should be avoided when possible.
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings 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!