Creating Variables from Excel
3 次查看(过去 30 天)
显示 更早的评论
Hello all!
I am trying to make my program more user friendly by having the user view all of the variables and define their values in an excel spreadsheet, with variable names in the one column and values in another. My hope was to create global variables using those variable names, in case the row values change or variables are added/removed. While I have found many ways of getting the data into the workspace, I cannot figure out how to create these variables using the names from the spreadsheet. Is there a way to define variables from strings or am I just wasting my time?
Thanks!
0 个评论
采纳的回答
Rick Rosson
2011-7-29
You can use the eval command to convert a string into a variable, although I generally recommend against using eval unless absolutely necessary.
For example:
eval( [ aStr ' = ' num2str(aValue) ';' ] );
For more information:
>> doc eval
HTH.
Rick
2 个评论
Ashish Uthama
2011-7-29
Richa, you could also consider dynamic structure field names:
aStr = 'myVar';
ssheetData.(aStr) = 1;
Walter Roberson
2011-7-29
Please do not use eval! Dynamic structure names are better. See also http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
更多回答(2 个)
Rick Rosson
2011-7-29
As an alternative to using Excel, have you considered creating a GUI in MATLAB for the users to enter the values of these variables? How many variables are there? Are all of them scalars, or are any of them arrays?
There are in general two ways to create a GUI in MATLAB. You can either create the GUI programmatically (on-the-fly), or using a tool called GUIDE that is provided with MATLAB. Depending on how many variables and how complex of a GUI you want to create, you may find that one approach is better than the other based on your requirements.
For more information about GUIDE:
>> doc guide
HTH.
Rick
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!