How can i create a variable in matlab that name stores in char variable?

The problem is I have a structure with various field names and I can get them as symbolic variables. I need to create a variable that name is the same as name of one field name of the structure. How can I do that?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de En. de 2011
Chances are that you do not need that and are advised not to do it. Please review the FAQ on this topic.

Más respuestas (2)

Jan
Jan el 26 de En. de 2011
Please consider Walter's suggestion. You can find a pile of questions concerning problems caused by this "poofing" methods. Creating variables dynamically is dangerous, confusing, hard to debug and slows down Matlab noticably. The wanted easier solution is to store the variables directly in a struct and avoid the conversion to and from single variables. Notice that "a(1)" can be a variable in one function and a private subfunction call in another function. In opposite to that "S.a(1)" accesses the variable stored in the field ever.
There is no "more simple" way to solve your problem. If you have to use EVAL, your approach includes a high complexity already. Therefore the best solution is to reduce the complexity of the data representation by packing the variables in a struct, array or cell array. Let me compare it with the question about a "simple" method to access variables, whose names are written with light yellow on white paper. Of course, ImageAnalyst can solve this, but it is a much better idea to simply choose a more computer friendly data storage model!
If you'd be grateful for a valuable advice, follow Waltern's suggestion and take into account that this topic was included in the FAQ, because hundrets and thousands of others have struggeled with the same problem also. The FAQ was written for you.
Kind regards, Jan
Alexis
Alexis el 26 de En. de 2011
Thank you. May be I'm not advised to do it, but the task is that I would like to create a function that could save workspace variables into the structure with the same fieldnames as varible's names, and then build function to download from structure those variables into workspace. It is like as 'save' 'load' functions but which operate with structures instead of files. I have found yet 'eval' function, that seems suit me. May be there is more simple way. I'd be very grateful if somebody advised me.

1 comentario

See for example Matt Fig's contribution,
http://www.mathworks.com/matlabcentral/fileexchange/26216-structure-fields-to-variables
Or see this contribution from Hoi Wong: http://www.mathworks.com/matlabcentral/fileexchange/23254-map-fields-of-a-structure-to-output-variables

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 26 de En. de 2011

Comentada:

Jan
el 7 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by