Using save(Filename) in a Callbackfunction of a button
Show older comments
I am programming a GUI and i want to save data in a callback function in matfile. I was under the impression, that if i use save only the functions workspace will be saved. Does hObject include all GUI objects in the function workspace?
save_pushbutton = uicontrol('Parent', tab ,'Style','pushbutton','Callback',@save_callback,...
'String','Save',...
'FontSize',8,...
'Units', 'normalized', 'Position',[.15 .05 .1 .05]);
editfield = uicontrol('Parent', tab ,'Style','edit','Callback',@input_control,...
'String','',...
'FontSize',8, 'TooltipString', ['Input must be numerical'], ...
'Units', 'normalized', 'Position',[.15 .8 .1 .05]);
function save_callback(hObject, eventdata)
x = get(editfield,'String');
save('save.mat');
end
In this example i want to save x in the mat file. Since i have a lot of variables i tried to keep it short and not name them all individually (save(Filename,x,y...).
Edit: Apparently there is a GUI workspace and GUI/callback workspace. I only want to save the GUI/callback.
Edit: Added more code
Accepted Answer
More Answers (1)
"Does hObject include all GUI objects in the function workspace?"
According to that, if you define all of your uicontrol elements under a property of your main gui class structure, hObject includes all items.
Actually first input of your callback is an instance of your main gui class object.
If you store uicontrol elements in a class property you can reach them everywhere.
For ex:
classdef myClass < handle
properties
guiHandles
someVariables
end
methods
% Constructor method
function myclass(obj)
obj.someVariables = {'bla bla'};
end
function createGUI(obj)
obj.guiHandles.mainFig = figure; %% creates a figure
obj.guiHandles.pushbutton = uicontrol(... %% creates a pushbutton inside that figure
'Parent',obj.guiHandles.mainFig,...
'Callback', @obj.pushbtnCallback,...
'String','OK',...
'units','normalized',...
'Position', [0.5 0.5 0.3 0.3]...
);
end
function pushbtnCallback(varargin) % Callback of the push button
% you can see each gui item which are stored in the guiHandles
% property of the class object. You can create any uicontrol
% element
myclassObjHandle = varargin{1}; % hObject -> main GUI object
pushbuttonObjHandle = varargin{2}; % source -> source object of this callback
eventData = varargin{3}; % eventData -> event data of the pushing button action
% you can save myclassObjHandle in here or you can choose to save it in command window or your script where you have called createGUI function.
end
end
end
To create myClass obj and its GUI call like below in your command window or in another script or function:
obj = myClass
obj.createGUI
% Do not forget to put debug point to pushbtnCallback function 1st line and then you will see what's inside the myclassObjHandle.guiHandles structure
% if you save obj in here it includes all gui items. It is the same object with myclassObjHandle.
1 Comment
Also you can read more about handle classes to learn usage:
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!