Insert a changing number into a variable in matlab?
38 views (last 30 days)
Show older comments
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 Comments
Answers (2)
James Tursa
on 11 Mar 2015
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:
Michael Haderlein
on 11 Mar 2015
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 Comments
Michael Haderlein
on 12 Mar 2015
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) )
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!