Insert a changing number into a variable in matlab?
30 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 12 Mar 2015
Modificato: Stephen23
il 19 Giu 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).
Risposte (2)
James Tursa
il 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
il 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 Commenti
Michael Haderlein
il 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) )
Vedere anche
Categorie
Scopri di più su Variables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!