Calling a class method from a GUI

12 views (last 30 days)
Hello there?
I am making a OOP test program that we can run at a GUI. The code for the class is
classdef test_1 < handle
properties
name
connectStatus = 0
end
methods
% Constructor
function obj = test_1(test_name)
obj.name = test_name
end
function status = getStatus(obj)
status = obj.connectStatus
end
function obj = setStatus(obj, newStatus)
obj.connectStatus = newStatus
end
end
end
When I run this on workspace, I found that it works fine. I can check the values of the attributes of the object and I can run the method suich as getStatus, setStatus.
Now I made a GUI and tried to run the method whenever I push a button on the GUI. But Matlab gives me error messages like
'Undefined variable "obj" or class "obj.getStatus"'.
Here, obj is the object that I created and getStatus is the method.
I think the problem is simple but I do not know how to solve this.
So, I will be very thankful if someone give me a tip.
Thanks.
  1 Comment
snow John
snow John on 2 Sep 2019
Hello sir,
I have met the same problem ,can you give me some cases as reference.
Your help will be appreciated.
Bset!
-John

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 29 Mar 2015
Wooshik - I suspect that what is happening is that you have defined your obj object either in the base workspace (outside of the GUI) or you have created it as a local variable in one callback or the Opening Function of your GUI and are trying to reference it in the push button callback. If either is the case, then the error message makes sense since obj is not part of the function workspace (see http://www.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html for an explanation of the workspaces).
If you want to reference the obj across callback within your GUI, and assuming that you have used GUIDE to create the GUI, then use the handles object to store this data. For example, if you create obj in the Opening Function as
function myGui_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for untitled
handles.output = hObject;
% create the obj object
obj = test_1('Any name');
% save obj as a field within handles
handles.obj = obj;
% Update handles structure
guidata(hObject, handles);
The order in the above is important - we create the object obj, save it as a field of handles, and then update the handles structure with guidata so that all other callback will have the updated version of handles.
Now in your push button callback you would do something like
function pushbutton1_Callback(hObject, eventdata, handles)
if isfield(handles,'obj')
myObj = handles.obj;
myObj.getStatus;
end
In the above, we are checking to see if the obj field exists within handles before trying to access it.
Try the above and see what happens!
  1 Comment
Wooshik Kim
Wooshik Kim on 30 Mar 2015
Thank you very much Geoff,
Your analysis is quite right. I defined the object in the base workspace because my intention is that several GUI's will access the same object and its methods and this object will manage how many GUI's are connected and left.
Right now, I am not sure whether your answer will exactly solve what I am trying to do, however your answer definitely gave me an idea of what I am going to next. I think I will try making another GUI that manages the object.
Thank you very much. I really appreciate your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!