2

I have a struct that contains some debug-options i.e: dbs = struct('db1', 0, 'db2', 1). And user can add more debug options to this struct (db3, db4 etc.) later, as well as change the value of existing fields db1, db2.

I want to create a GUI dbGUI with uimenu Debug options. dbGUI should take dbs as input argument, so i put varargin to my GUI's OpeningFcn, so i can call the GUI from command window like dbGUI(dbs):

function dbGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.dbNames = fieldnames(varargin{1});
handles.dbVal = cellfun(@(i) varargin{1}.(i), handles.dbNames);

The uimenu "Debug options" should show submenus corresponding with dbs's fields. What i want is: how to auto-update the submenus when user modifies the struct dbs from outside of the GUI (means in Matlab command window)?

As suggested from @marco wassmer below, the struct dbs should be declared as a handle class inherited from dynamicprops and using addlistener. The problem is: when the user changes dbs in Command Window, the function updateFoo cannot get the dbGUI's handles, thus cannot update the value to handles, and finally the uimenu_CallbackFcn cannot update the new fields and values.

Note: I tried to use GUI programmatically, but i prefer GUIDE since it's easier for me to layout the components.

2 Answers 2

1

You have two options:

  • Update the menue every time the user clicks it, something like this:

    handles.dbs_menue = uimenu(f,'Label','Debug options','Callback','@foo')
    
    .
    .
    .
    
    function foo(hObj)
        dbs=getVariable('base','dbs')
        handles.dbO = fieldnames(dbs.dbOptions)
        % and replace all menue items...
    

according to matworks this is not recomended, source: http://ch.mathworks.com/help/matlab/ref/uimenu-properties.html#prop_Callback

  • The otehr way would be to make dbs into a handle object and add a listener in the GUI. Maybe a bit more elaborate than you had in mind, but here it goes. Don't worry it will work almost the same as a struct... first you need to make a handle object and therefore a class:

        classdef dbs_class < dynamicprops
    properties (Access='private')
        lhs=[];
    end
    events
        StateChange
    end
    methods
        function obj=dbs_class(a)
            addlistener(obj,'PropertyAdded',@eventPRadd);
            %addlistener(obj,'StateChange',@(varargin) disp('change'));
        end
    
        function eventPRadd(obj,evnt)
            names=fieldnames(obj);
            meta_p=obj.findprop(names{end});
            meta_p.SetObservable=true;
            obj.lhs.(names{end})=addlistener(obj,names{end},'PostSet',@(varargin) notify(obj,'StateChange'))
            obj.(names{end})=0;
            notify(obj,'StateChange')
        end
    
        function removeprop(obj,name)
            meta_p=obj.findprop(name);
            delete(meta_p)
            delete(obj.lhs.(name))
            notify(obj,'StateChange')
        end    
    end
    

    end

Then make one of these:

dbs=dbs_class(1);

This, the variable dbs that is, 'behaves' now almost like a struct, execpt when some one changes one of the properties, the event StateChange will be triggerd.

In the opening function all you need to do is add the listener and callback to the objects event 'StateChange':

function gui_test(dbs_in)
handles.fig=figure('Tag','fig');
addlistener(dbs_in,'something_changed',@updateFoo);
handles.dbO = fieldnames(dbs_in);
handles.dbmenu= uimenu('Label','Debug');
for n=1:numel(handles.dbO)
    if dbs_in.(handles.dbO{n})==1
        uimenu(handles.dbmenu,'Label',handles.dbO{n},'Callback',@(varargin) disp(['Debugg mode :' handles.dbO{n}]));
    end
end
guidata(handles.fig, handles);

function updateFoo(src,~)
handles=guidata(findobj('Type','Figure','Tag','fig'))
handles.dbO = fieldnames(src);
delete(get(handles.dbmenu,'Children'))
for n=1:numel(handles.dbO)
    if src.(handles.dbO{n})==1
        uimenu(handles.dbmenu,'Label',handles.dbO{n},'Callback',['disp([''DebuggingMode :'  handles.dbO(n) '])']);
    end
end
guidata(handles.fig, handles);

Source: http://ch.mathworks.com/help/matlab/matlab_oop/learning-to-use-events-and-listeners.html

7
  • thanks for your suggestion. But i not only want to modify the value of fields, but also add more fields. I found something usefull with dynamicprops, but couldnt manage to make it run ...
    – scmg
    May 16, 2015 at 17:22
  • @scmg I changed the anwser to use the dynamicprops -class, thanks for the input, quite intresting this class May 16, 2015 at 19:25
  • thanks again, but unfortunately, your sample code for the class has so many minor bugs, and i finally dont know the GUI doesnt work because of the errors in GUI's functions, or of the errors in the class ...
    – scmg
    May 16, 2015 at 21:51
  • well, the main problem is: if you modify dbs_class, then you are in Matlab's command window, and handles=guidata(gcf) in your updateFoo just creates new blank figure than gives you the handle of your GUI.
    – scmg
    May 16, 2015 at 23:00
  • 1
    ok, i think i got it now, to find the handles use handles=guidata(findobj('Type','Figure','Tag','dbGUI')). I assuem dbGUI is the tag of your Figure, you can change it with the propertyinspector in guide May 17, 2015 at 13:44
0

I just found out that it could be much more simpler:

I just declare dbs as a handle class. Then in GUI's OpeningFcn, assign a field to dbs: handles.myvar = varargin{1};. Since dbs now is a handle class, everything i do with dbs outside the GUI (i.e in Command Window) will also be updated to the GUI's handles.

function dbGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.var = varargin{1};
% other fields in handles
guidata(hObject, handles);

function db_menu_Callback(hObject, eventdata, handles)
disp('Updated variable: ');
disp(handles.var);
% here do whatever i want ....
guidata(hObject, handles);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.