How to pass data between GUI and different .m file

I have a GUI that allow the user to choose one of filtering type from the popup menu.
The popup menu program is written in different .m file.
In only one of the selection, what should happen are:
-one message box should popup that says the user should key-in the parameter inside the box below.
-the appropriate box that I set to be not visible should be visible when the selection is being choose.
Then, when the user have key-in the parameter that is needed and click the pushbutton to do the filtering, the result should be plotted.
My problem is that I want to do the function inside the popup menu .m file but I can't access the data that is being key-in inside the GUI. I've tried using this codes but it didn't work:
if get(handles.cwt_apply,'Value')
scale=str2num(get(handles.cwt_scale,'String'));
switch get(handles.cwt_menu,'Value')
case 1
for i=1:length(datain(:,1))
dataout = cwt(datain(i,:),scale,'cmor1-1.5');
end
case 2
for i=1:length(datain(:,1))
dataout = cwt(datain(i,:),scale,'morl');
end
case 3
for i=1:length(datain(:,1))
dataout = cwt(datain(i,:),scale,'haar');
end
end
end
The error is Undefined variable "handles" or class "handles.cwt_apply".
Though I've manage to make the box visible, but the codes is written in the main GUI program. Does this affect the passing of data? The message box is also written in the main GUI program. This is the codes:
if get(handles.filter_menu,'Value') == 10;
h = msgbox('For CWT, please choose appropriate parameter inside the box below');
set(handles.cwt_scale,'Visible', 'on');
set(handles.cwt_menu,'Visible','on');
set(handles.text52,'Visible','on');
set(handles.cwt_apply,'Visible','on');
else
set(handles.cwt_scale,'Visible','off');
set(handles.cwt_menu,'Visible','off');
set(handles.text52,'Visible','off');
set(handles.cwt_apply,'Visible','off');
end;
Besides that, the message box has a red line graph that suppose to be plotted on the appropriate axes. I can't seem to make it disappear.
Thanks in advance for the help..

 Accepted Answer

We need to see the code for the popup.m file, in particular the 'function' header or the place you are initializing handles. And we need to see the code near the place that would invoke popup.m

5 Comments

Change your current line,
handles.zoomdata = filtering(handles.zoomdata,get(handles.filter_menu,'Value'));
to
handles.zoomdata = filtering(handles.filter_menu, handles.zoomdata,get(handles.filter_menu,'Value'));
And change your existing
function [dataout] = filtering(datain, type)
to
function [dataout] = filtering(hObject, datain, type)
And then at the top of that routine, add
handles = guidata(hObject);
I've tried what you've told me but it still got some error which is
Error in ==> filtering at 4
switch type
Error in ==> GUI>signal_filtering_btn_Callback at 1144
handles.zoomdata = filtering(handles.zoomdata,get(handles.filter_menu,'Value'));
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)GUI('signal_filtering_btn_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Another thing, does the handles=guidata(hObject) is being place on top of the main pushbutton callback?
Sorry for asking too much from you. I really need help with this.
Thanks a lot.
You missed where I said that the call to filtering had to have the additional parameter passed to it.
handles.zoomdata = filtering(handles.filter_menu, handles.zoomdata, get(handles.filter_menu,'Value'));
Notice that is now 3 parameters, not 2.
Sorry for my mistakes. I've tried but it still have some error..
Did i miss anything again? The error now are:
??? Undefined variable "handles" or class "handles.cwt_apply".
Error in ==> filtering at 102
if get(handles.cwt_apply,'Value')
Error in ==> GUI>signal_filtering_btn_Callback at 1144
handles.zoomdata = filtering(handles.filter_menu,
handles.zoomdata,get(handles.filter_menu,'Value'));
Thanks a lot in helping me..
Did you add
handles = guidata(hObject);
as the first line of the routine, right after the 'function' statement ?

Sign in to comment.

More Answers (3)

Use the guidata() function.
You would tag a callback to each control. Depending on the control, it would either read the guidata or write to the guidata (to update the current state).
Typically I use a struct in my guidata to store the different information.
To me, it sounds like you need to make the handles of the items in the GUI part of the guidata.
Thanks Walter Roberson,
I'm still new with MATLAB, so I don't really understand what is the function header. I'm using the "guide" to built my GUI.
Basically when the user choose any type of filtering, the result will straight away be plotted on the axes.
Only for this one case in the popupmenu, the message will be prompt and the visibility of the parameter boxes will be "on".
After that, when the user key-in the parameter, click the pushbutton in the panel, the result will be plotted on the axes.
The codes in the main GUI for the popupmenu is this:
handles.zoomdata = filtering(handles.zoomdata,get(handles.filter_menu,'Value'));
*the popupmenu .m file name is filtering
the header for the popupmenu .m file is
function [dataout] = filtering(datain, type)
then, I used switch for each popup menu selection and place the function of filtering inside it.
I don't think I have initializing the handles at all.
Sorry for my ignorance with MATLAB and if what I wrote does not giving the information that you want.
Thanks,
Thanks Jason Kaeding for the answer.
But can you tell me how to do it.
Seriously, I don't even know how to write the codes. I only know how to do the GUI using guide and do the simple callbacks.
Sorry for troubling you.I really appreciate your help.

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!