I am working on GUI. I want to store data in extra fields created in handles structure. However, I don't know how to update handles structure properly when callback function ends. Please, give any advice.
My simplified program
GUI
function simple_gui(hObject, h)
h.fig = figure(...
'Units','pix',...
'Position',[50 50 500 400],...
'Visible','default',...
'Name','GUI',...
'NumberTitle','off',...
'Resize','on');
table = {'1' , '2', '3' , '4', '5', '6', '7', '8', '9', '10' };
h.number = uicontrol(...
'Units','characters',...
'Max',10,...
'Min',1,...
'String',table,...
'Style','popupmenu',...
'Value',1,...
'Position',[37.4 28.3846153846154 19.4 1.61538461538462],...
'BackgroundColor',[1 1 1]);
h.edit1 = uicontrol(...
'Units','pix',...
'String','0',...
'Style','edit',...
'Position',[180 280 50 20],...
'BackgroundColor',[1 1 1],...
'FontSize',10);
h.edit2 = uicontrol(...
'Units','pix',...
'String','0',...
'Style','edit',...
'Position',[180 255 50 20],...
'Children',[],...
'FontSize',10);
h.edit3 = uicontrol(...
'Units','pix',...
'String','0',...
'Style','edit',...
'Position',[180 230 50 20],...
'FontSize',10);
Main code:
h.parameter1 = zeros(1,10);
h.parameter2 = zeros(1,10);
h.parameter3 = zeros(1,10);
h.signal_no = 0;
h.number.Callback = {@number_Callback, h};
h.edit1.Callback = {@parameter_change_Callback, h};
h.edit2.Callback = {@parameter_change_Callback, h};
h.edit3.Callback = {@parameter_change_Callback, h};
guidata(h.fig, h);
function number_Callback(hObject,eventdata, h)
h = guidata(hObject);
h.signal_no = hObject.Value;
k = h.signal_no;
h.edit1.String = h.parameter1(k);
h.edit2.String = h.parameter2(k);
h.edit3.String = h.parameter3(k);
guidata(hObject,h);
function parameter_change_Callback(hObject,eventdata, h)
h = guidata(hObject);
k = h.signal_no;
h.parameter1(k) = str2double(h.edit1.String);
h.parameter2(k) = str2double(h.edit2.String);
h.parameter3(k) = str2double(h.edit3.String);
guidata(hObject, h);
Type guide in command window. A new GUI dialog box will appear. In the dialog box you will select the existing GUI project. To to the tab and you will find the gui file which you want to edit.
EventData class is the base class for all data objects passed to listeners. When you trigger an event using the notify handle class method, MATLAB® assigns values to the properties of an event. EventData object and passes that object to the listener callback function (the event handler).
The GUI Options Dialog Box Access the dialog box from the GUIDE Layout Editor by selecting Tools > GUI Options. The options you select take effect the next time you save your UI.
Update from Notifications If a more recent version is available, a notification displays in MATLAB when you start the program. To install the MATLAB update, click the notification icon and then select Install Update.
In summary:
Call guidata(handleObject, varToStore)
(documentation) at the end of GUI callback functions to ensure updates to one modified variable are stored. Here, handleObject is either your figure's handle or a child of it, and varToStore is the updated variable you want stored; it is often a structure.
The syntax for retrieving stored data from a figure or child handle
:
handles = guidata(gcbo); % gcbo will get the callback object (instance of handle class).
handles.propToUpdate = handles.propToUpdate+1;
guidata(gcbo,handles); % stores the updated struct
In addition:
You won't see the changes from your popupmenu reflected in your edit boxes in the GUI with the current code because you are assigning the numeric values to a edit handle's String
field. You call str2double()
when taking values this field, and just need to do the reverse (num2str()
) to get the displayable values back in. Here's the updated code with a simplified callback declaration
h.number.Callback = @number_Callback;
function number_Callback(hObject,~)
h = guidata(hObject);
h.signal_no = hObject.Value;
k = h.signal_no;
h.edit1.String = num2str(h.parameter1(k));
h.edit2.String = num2str(h.parameter2(k));
h.edit3.String = num2str(h.parameter3(k));
guidata(hObject,h);
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With