How to pass data in a gui and refresh/reload a list box

hello, I trying to load a mat file into a GUI and then have to user choose the variable in the file in order to pass it into a function. So far, I have managed to load the file using "uigetfile" and then "load" to assign all the variables in the file into a struct variable,and then I got the names of the variables (fields) using "fieldnames".
How can I display those variable names in a list box and assign the one chosen by the user to a new variable and pass it into my function?
thanks in advance I'm not a programmer and I if the solution is too complex odds are that I'll not get it, however if it is simple, I will be thank full for your help.

 Respuesta aceptada

Rik
Rik el 17 de Mzo. de 2018
You can set the output from fieldnames as the String property of the listbox. You can use indexing with the Value property in the callback to set it to your variable:
function listboxcallback(hObject,eventdata,handles)
fields=get(hObject,'String');
val=get(hObject,'Value');
handles.yourvariable=handles.loadedstruct.(fields{val});
guidata(hObject,handles)

4 comentarios

i dont know how to use the guidata, i have been using setappdata and getappdata.. will that also work?
Rik
Rik el 17 de Mzo. de 2018
Sure, that should work as well.
guidata saves a struct to a figure, so you can easily share data between callbacks. If you input something that is not a figure, it will cascade up until it find the parent figure.
thank you for your help, I think it is working! Is it normal that it is so slow? I load a mat file and it takes more than 5 sec to load the different values in the list box
Rik
Rik el 17 de Mzo. de 2018
The bulk of the time should be spent loading the .mat from disk. Setting the String property should only take a few ms. It is possible to only see the effect a bit later, if other callbacks are still being processed and Matlab postpones the graphics update until the callbacks are finished (or you use drawnow)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de Mzo. de 2018

Comentada:

Rik
el 17 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by