How do I make assignments to my workspace using variable names and values in a spreadsheet?
Mostrar comentarios más antiguos
I have an excel file that has names of structures in the top row, names of fields of those structures in the 2nd row and in the remaining rows, values to be assigned to those fields in a columnwise manner. How can I read this into arrays of values for each field? Assume the values are floats.
Example: 4x4 Excel file might look like
P P V V
X Y X Y
0 1 1 2
3 2 1 2
Result should be that I have in my workspace
P.X = [0 3];
P.Y = [1 2];
V.X = [1 1];
V.Y = [2 2];
Can I do this without using eval()?
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 12 de En. de 2016
Editada: Walter Roberson
el 12 de En. de 2016
1 voto
Technically you can do it without using eval(), but it would require writing a routine that used assignin('caller') to assign the value into the workspace you are already in, with much the same effect.
Doing what you want to do is not a good idea. See http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
I recommend that you push everything down one level into a master structure, so instead of creating P.X and so on you would create (say) vars.P.X . This can be done easily using dynamic field names without the risks over eval() or of a structure name in the file accidentally being a MATLAB reserved word or accidentally being the name of a variable or function you are using in the reading routine itself. For example suppose you read your lines using fscanf() and the user has requested a structure name of 'fscanf', then as soon as you created that as a variable you would no longer be able to call the fscanf routine.
4 comentarios
John Petersen
el 12 de En. de 2016
Walter Roberson
el 12 de En. de 2016
If you are applying them to a model then you would be using set_param(), which does not care what the MATLAB-level variable name is, only that you give the right string for the Simulink variable name and that you give a valid value for the parameter.
Or are these variables for a From Workspace block?
John Petersen
el 12 de En. de 2016
Editada: John Petersen
el 12 de En. de 2016
Walter Roberson
el 13 de En. de 2016
This is getting beyond what I have read about Simulink. Perhaps if you used a Model Workspace layer, and specified a .mat file that has the variables stored? A MATLAB file is also an option there but it is not obvious what form that file would have to take.
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!