Add to listbox in GUI from .mat file

My GUI currently has 3 push buttons and a list box. two buttons calls for the user to choose their desired data file. these data files all have the same format, however they have a different number of data sets. in the GUI when i press the pb2, it saves all the variables used that were calculated. i would like to use one of the variables which has X ammount of data set, and read that data, and write that data to the list box. here is the code i tried, i get errors about the structure and i'm quite stumped. I'm following GUIs_FEX GUI layouts and creating from the base up because I don't really understand how to do calculations in the GUIDE file. it tells me variables don't exist, and variables wiped out every function. If somone has time to discuss with me and help me that would be great, thank you!
S.ls = uicontrol('style','list','unit','pix','position',[50 50 150 450],...
'min',0,'max',2,'string','Frequencies','callback',{@ls_call});
.
.
.
load('F06_Variables')
for i=1:R
S= varargin{3}
oldstr = get(S.ls,'string');
addstr = {frequency(i)};
set(S.ls,'str',{oldstr{:},addstr{:}});
end

2 Comments

I do not understand "variables which has X ammount of data set". Whenever you mention in a forum, that you get errors, post a copy of the error messages also. We are good in solving problems, but bad in guessing them.
i meant that the number of data sets would change upon the user choosing different files. but i've figured it out. im now attempting to follow jiang answer so that my gui doesnt do the calculations twice

Sign in to comment.

 Accepted Answer

I think the main issue is that the variables only has local scope when loaded in a callback function. If you only have one GUI, the easiest way to access data when you jumping in and out functions is using GUIDATA for the specific items. For example, set your data as the guidata of the whole GUI:
s=load('F06_Variables');
guidata(figure_handle,s); %remember to update the guidata every time you load a new .mat file
%%%%%%%%%%in pushbutton or listbox callbacks
S=guidata(figure_handle); %you can add 'figure_handle' as an input for the callbacks, or obtain it by get(hObject,'Parent') if figure is the parent of pushbutton or listbox
And there're other ways also available for your purpose, details on the link below: http://www.mathworks.cn/cn/help/matlab/creating_guis/ways-to-manage-data-in-a-programmatic-gui.html

6 Comments

using GUIDE as my setup,i got the program to create the list as ive wanted. the problem now is using the variables in other functions. so the button the user presses first grabs the files which contains the data i want, and then wrties the frequency to the list box, now in the code caculation part, i do a save('All_Varibales') now in the listbox call back loop i did as you instructed,
get(hObject,'Parent');
guidata(figure_handle);
AV=load('all_Variables');
guidata(figure_handle,AV);
but i get this error
??? Undefined function or variable 'figure_handle'.
Error in ==> Modal_Lines>listbox_Callback at 93
guidata(figure_handle);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Modal_Lines at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Modal_Lines('listbox_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I don't think i'm completely understanding
Here "figure_handle" means the handle of your figure. Adjust the code to your needs is your job.
if i just want to use the variables in AV to do calculations in the function why do i need to load it to my figure handle though? i thought figure handle was for the graph. my list box call back functions does calculations based on the choice chosen and graph those so don't i have to set the gui data to somethign else? since im not plottign my variables directly from the list choice?
That is because you need to jump in and out the callback to do the calculation, which means you need to store your variables at a higher level, parent of your listbox and pushbutton, or the handle to the whole UI--the figure handle.
How do i identify what my figure handle is called? When i look it up in mathlab it says GCF, so does that mean what i want to do in the listbox call back is, listh=gcf; AV=load('All');guidata(listh,AV);
The figure handle means the handle to your GUI. It depends on how you create the GUI.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 29 Jan 2013

Community Treasure Hunt

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

Start Hunting!