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!