Insert a changing number into a variable in matlab?

35 ビュー (過去 30 日間)
matlabuser12
matlabuser12 2015 年 3 月 11 日
編集済み: Stephen23 2019 年 6 月 19 日
I am defining some variables based on gui inputs, like so:
day1name=get(handles.day1name,'String');
day1start = datenum(get(handles.day1start,'String'));
day1end = datenum(get(handles.day1end,'String'));
day1data =Data(day1start:day1end,:);
day1datamodified = day1data*xyz
where day1datamodified is an nxn double array.
Basically I want to do this for several days, and several different entries. other than copy pasting this for my 15 days and making 15 different variables. is there a way to make the day1start say day2start, sort of like a day(i)start where i=1:15? then build a bigger structure array with little data arrays in it for each day that are labeled by the day1name entry which I can then manipulate and plot to my hearts content? I can probably use some for loop script for this, but how do I insert a variable into my variables? fyi, Data is a larger excel file i imported which has a time column that I am taking chunks of data from with the start/end times of the day.
  3 件のコメント
matlabuser12
matlabuser12 2015 年 3 月 11 日
that's helpful. what do you find confusing?
Stephen23
Stephen23 2015 年 3 月 12 日
編集済み: Stephen23 2019 年 6 月 19 日
Ugh, do not create dynamically name variables:
As Michael Haderlein has already recommended, you should use a non-scalar structure instead: this would be perfectly suited for your data, with different fields (which can by dynamically named).

サインインしてコメントする。

回答 (2 件)

James Tursa
James Tursa 2015 年 3 月 11 日
Yes there is a way to do this using the eval function, but you will be buying a LOT of headaches with your programming in the future if you do this. Cell arrays and/or structs are typically the way to go here. E.g., see this post:
  1 件のコメント
matlabuser12
matlabuser12 2015 年 3 月 11 日
I tried to follow that example but I cannot use the for loops they use without being able to change my variable name. the edit box names are fixed, hence my question.

サインインしてコメントする。


Michael Haderlein
Michael Haderlein 2015 年 3 月 11 日
You can use structure arrays for that:
>> day(1).name='Monday';
>> day(1).start=9;
>> day(2).name='Tuesday';
>> day(2).start=8.5;
>> [day.start]
ans =
9.0000 8.5000
>> {day.name}
ans =
'Monday' 'Tuesday'
Of course, you can fill the content of the structure with a loop. If your data is in an Excel file, you might find the functions xlsread() and cell2struct() useful. The respective documentation in Matlab is pretty helpful.
  2 件のコメント
matlabuser12
matlabuser12 2015 年 3 月 11 日
i already have the data from xlsread, i take different chunks of it (some even overlapping) and create new variables like i have shown for one case above. How can i do it for a large number of cases and varying entries? can i label my gui box with day(1) and then make it get(handles.day(i)) and it will still work?
Michael Haderlein
Michael Haderlein 2015 年 3 月 12 日
The role of the GUI here is rather unclear. I guess you have one textbox in which you want to enter the index (let's say 2) and a number of labels or protected textboxes which will be filled with the data (in my example, 'Tuesday' and 8.5). Is that right? Then you can simply convert the string in the textbox to a number (str2double) and set the string values of the labels to the respective values (something like set(handles.txtname,'string',day(index).name) )

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by