Insert a changing number into a variable in matlab?

59 views (last 30 days)
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
Stephen23
Stephen23 on 12 Mar 2015
Edited: Stephen23 on 19 Jun 2019
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).

Sign in to comment.

Answers (2)

James Tursa
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:
  1 Comment
matlabuser12
matlabuser12 on 11 Mar 2015
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.

Sign in to comment.


Michael Haderlein
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
matlabuser12
matlabuser12 on 11 Mar 2015
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 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) )

Sign in to comment.

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!