Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab GUI: How to update handles structure?

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

  • Set number of signal (1-10). Each signal has 3 parameters.
  • Read parameter for selected signal from array created in handles structure (default are zeros).
  • Edit parameter, update the array.

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);
like image 965
K.Ramel Avatar asked May 01 '17 15:05

K.Ramel


People also ask

How do I edit a GUI fig in MATLAB?

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.

What is EventData in MATLAB GUI?

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).

Where is GUI options in MATLAB?

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.

How do I update my MATLAB code?

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.


1 Answers

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
like image 69
informaton Avatar answered Oct 02 '22 23:10

informaton