guidata handles + started using Matlab classes - why do I have to update (pass by value/reference?)

Hi all,
A LOT has been written about guidata and handles, but I couldn't find exactly the answer to my question. So I'm learning to use object-oriented programming in Matlab and I am making a GUI and in the end of the gui (I call it MainFigureWindow) OpeningFcn(hObject, eventdata, handles, varargin) I/we have:
handles.guiDataFile = 'someStuff.txt';
handles.utils = utilities(handles); % utilities is a class
handles.myMachineClass = myMachineClass( parameter1, parameter2, handles );
guidata(hObject,handles); % saving my "global variables"
Notice that I pass "handles" into the classes, so I always have access to my "global variable" (I'm sure you understand what I mean, although maybe technically it's not really a "global variable")... Then I have (simplified) my first class (utilities.m)-definition:
classdef utilities < handle % a handle class (pass by reference)
properties
ourHandles
end
methods % non-static, only for this particular GUI
function obj = utilities(ourHndls) % constructor
% pass by reference or pass by value? Seems like pass by value, how to get pass by reference?
obj.ourHandles = ourHndls;
end % constructor
And I have my second class-definition (i.e. "myMachineClass.m" in this simplified example):
classdef myMachineClass < handle % a handle class (pass by reference)
% ==================================================
properties % default public
ourHandles
MachNum % empty [] default value, use constructor
end
function obj = myMachineClass(ourHndls) % constructor
% pass by reference or pass by value? Seems like pass by value, how to get pass by reference?
obj.ourHandles = ourHndls;
end % constructor
I use "handles.myUtilityFunctions = utilities(handles);" and "handles.myMachineClass = myMachineClass( parameter1, parameter2, handles );" so I should have access to all data everywhere...
In the GUI I have 3 checkboxes. The user must confirm some output and after the 3 checkboxes are all checked, the program continues to the next stage. Each checkbox callback function (in the main GUI-file) is like (num1_Ok_checkbox_Callback, num2_Ok_checkbox_Callback and num3_Ok_checkbox_Callback are the same, I don't want to show all - I'm just calling the utility function to check the "Value" of all 3 checkboxes):
function num1_Ok_checkbox_Callback(hObject, eventdata, handles)
handles.utils.checkAllcheckboxes()
Now we're leaving the main GUI ".m"-file and going into the utility ".m"-file (namely the "checkAllcheckboxes"-function):
function checkUVWcheckboxes(obj)
if obj.ourHandles.num1_Ok_checkbox.Value & ...
obj.ourHandles.num2_Ok_checkbox.Value & ...
obj.ourHandles.num3_Ok_checkbox.Value
obj.ourHandles.some_confirmed_text.String = "OK, confirmed!"
obj.updateListboxes();
else
obj.ourHandles.some_confirmed_text.String = "You did not confirm yet"
end
The program works pretty ok, so far. But now I want to update a listbox so I'm checking the "obj.ourHandles" - which in my opinion SHOULD correspond 100% to the "handles"-variable in the "main .m GUI"-file. But it doesn't... It seems like the obj.ourHandles-object contains references to almost everything, including "handles.guiDataFile", i.e. remember from my OpeningFcn:
handles.guiDataFile = 'someStuff.txt';
handles.utils = utilities(handles); % utilities is a class
handles.myMachineClass = myMachineClass( parameter1, parameter2, handles );
I cannot see the "handles.utils" nor "handles.myMachineClass", which I think is weird - because when I can see "handles.guiDataFile" it means the "guidata(hObject,handles)" worked - but not fully, i.e. at least to some degree... This behaviour, I don't understand... However, after reading a lot - searching in the forums, I discovered this temporary "solution":
readSavedVars = guidata(obj.ourHandles.MainFigureWindow);
Now I can type "readSavedVars.utils" and "readSavedVars.myMachineClass" and suddenly I have access again to the two new class-objects I've made and want access to... It is a solution, but I would prefer not having to use "readSavedVars = guidata(obj.ourHandles.MainFigureWindow);" to access the original "handles"-data.
My question is: Why is it that I have to use "readSavedVars = guidata(...)" when I've saved obj.ourHandles which should contain the same data as "handles"? Is it something about pass-by-value / pass-by-reference? In that case, I wish to have my class constructors save the "handles"-variable (to ourHandles) as a pass-by-reference, how would I accomplish this?
I hope someone can help me understand this better and I hope you think I've explained this properly with this minimal example, thanks!

Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2016a

Asked:

on 7 Aug 2018

Community Treasure Hunt

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

Start Hunting!