Insert a changing number into a variable in matlab?

29 visualizaciones (últimos 30 días)
matlabuser12
matlabuser12 el 11 de Mzo. de 2015
Editada: Stephen23 el 19 de Jun. de 2019
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 comentarios
matlabuser12
matlabuser12 el 11 de Mzo. de 2015
that's helpful. what do you find confusing?
Stephen23
Stephen23 el 12 de Mzo. de 2015
Editada: Stephen23 el 19 de Jun. de 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).

Iniciar sesión para comentar.

Respuestas (2)

James Tursa
James Tursa el 11 de Mzo. de 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 comentario
matlabuser12
matlabuser12 el 11 de Mzo. de 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.

Iniciar sesión para comentar.


Michael Haderlein
Michael Haderlein el 11 de Mzo. de 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 comentarios
matlabuser12
matlabuser12 el 11 de Mzo. de 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 el 12 de Mzo. de 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) )

Iniciar sesión para comentar.

Categorías

Más información sobre Variables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by