Interchange variable names and strings
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Timothy Cale
el 21 de Feb. de 2012
How do I convert a string to a variable name (and vice versa)?
I want to read in a string, and use it as a variable name, such as when I load a *.mat, in which the variable names depend upon which program generated/saved the *.mat.
I can see it useful to convert a variable name to a string.
0 comentarios
Respuesta aceptada
Walter Roberson
el 21 de Feb. de 2012
<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Consider using
S = load('YourMATFile.mat');
the_var_names = fieldnames(S);
second_var = S.(the_var_names{2});
You can slice, dice, trice, even julienne!
2 comentarios
Walter Roberson
el 23 de Feb. de 2012
the_var_names in the above code would contain the variable names.
I just pulled out the second one as an example to show how you can get at the data.
Más respuestas (1)
Doug Hull
el 21 de Feb. de 2012
We need more details on what you are doing, but this is likely a bad idea. Your code is going to be very difficult to write like this, and you will be fighting MATLAB along the way quite a bit.
There are probably better ways of doing this than allowing users to define the internal variable names to your program. You would need to validate the names, write your code to handle unknown variable names, etc. There is likely a better way.
Ver también
Categorías
Más información sobre File Operations 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!