0

I am new to Matlab, I want to call a function in different GUI without sending any arguments( from one GUI to another).

Example:

gui1.m

function some_ui_OpeningFun(hObject, eventdata, handles, varargin)

a = 10;
setappdata(0,'a',a);

gui2('pushbutton1_Callback'...) %<- to call the function in the second gui

handles.output = hObject;

guidata(hObject, handles);

gui2.m

function pushbutton1_Callback(hObject, eventdata, handles)

b = getappdata(0,'a');
disp(a);

so i want to call the function pushbutton1_Callback from gui2.m.

I have tried using gui2('pushbutton1_callback',handles,.....) which was give in the GUID comments. But could't get it.

Can anyone tell me how to do so?

thanks in advance.

2
  • If you want a function to be accessible from outside, you should place it in its own file and give the file the same name as the function, in your example pushbutton1_Callback.m. Commented Feb 9, 2015 at 4:14
  • @jadhachem both example above are made from GUIDE, this two are only the examples. How would i do that if i call it from a different GUI. you mean to say these two are function which are NOT. Commented Feb 9, 2015 at 4:21

1 Answer 1

0

If you want to access a GUI (gui2) from another GUI (gui1), you need some reference to gui2 in gui1, this can be done in several ways; for example by requiring that the figure handle to the gui2-object if given as an input parameter when you run gui1 (EDIT: when you run gui1 you should run it with a parameter for example like this gui1(f), where f is the figure handle for a gui2 figure - you can get this figure handle by running gui2 like this: f = gui2) e.g. add the following line after the function some_ui_OpeningFun(hObject, eventdata, handles, varargin)-line:

handles.othergui = varargin{1};

Then you can call the functions of the other gui as follows:

otherhandles = guidata(handles.othergui);
gui2('pushbutton1_Callback',otherhandles.pushbutton1,eventdata,otherhandles);

I hope this answers your question!

Sign up to request clarification or add additional context in comments.

6 Comments

Nope, not working. I tried doing that but i get Index exceeds matrix dimensions. Error in Neucube>Neucube_OpeningFcn (line 104) handles.othergui = varargin{1}; Error in gui_mainfcn (line 221) feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:}); Error in Neucube (line 49) gui_mainfcn(gui_State, varargin{:});
I tried other ways of it by doing Neucube('pushbutton5_Callback',hObject, eventdata, handles); Now the problem is that in Neucube under pushbutton5_Callback i have predict_time_length = str2double(get(handles.predict_time_length,'string')); which gets the value from gui text box. but here i get an error after I call it from diff GUI as follows
Reference to non-existent field 'predict_time_length'. Error in Neucube>pushbutton5_Callback (line 136) predict_time_length = str2double(get(handles.predict_time_length,'string')); % training persentage Error in gui_mainfcn (line 96) feval(varargin{:}); Error in Neucube (line 49) gui_mainfcn(gui_State, varargin{:}); Error in training>stop_training_Callback (line 234) Neucube('pushbutton5_Callback',hObject, eventdata, handles); Error in gui_mainfcn (line 96) feval(varargin{:}); Error in training (line 42) gui_mainfcn(gui_State, varargin{:}); Error in
@(hObject,eventdata)training('stop_training_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating uicontrol Callback
I have asked the same question here stackoverflow.com/questions/28407121/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.