user interface - updating a function in MATLAB -
the code below works. can copy-paste code in case don't understand ask below. in gui, have pushbutton, popupmenu , edittext. pushbutton enables adding new strings popupmenu answers obtained inputdlg. strings in popupmenu have additional information defined in windowtype function. when scrolling between popupmenu list, pmh_call calls windowtype function , displays information on edittext. information on windowtype function predefined. however, want add new window types , information function. first time develop gui. so, may have started saving new info business function wrong. appreciated.
function [] = mygui(varargin) s.fh = figure('visible','on','numbertitle','off','name','gunebakan gui',... 'units','pixels','position',[500 500 200 100]); s.pbh_definewindow = uicontrol(s.fh,'style','pushbutton','string','define window',... 'horizontalalignment','center','backgroundcolor',[0.6602 0.0234 0.2539],... 'position',[10 80 100 20]); s.pmh_window = uicontrol(s.fh,'style','popupmenu','string',{'type1','type2'},... 'value',1,'backgroundcolor','w','position',[10 50 150 18]); s.eth_windowlength = uicontrol(s.fh,'style','edit','string','4','position',[10 10 50 20]); set(s.pbh_definewindow,'callback',{@pbh_call,s}); set(s.pmh_window,'callback',{@pmh_call,s}); function [] = pmh_call(varargin) s = varargin{3}; string = get(s.pmh_window,{'string','value'}); string = string{1}(string{2}); [ windowtypeinfo ] = windowtype( string ); str = num2str(windowtypeinfo.length); set(s.eth_windowlength,'string',str); function [] = pbh_call(varargin) s = varargin{3}; error = 1; prompt = {'type name:','type length:'}; name = 'yeni agac tanimlama'; numlines = 1; defaultanswer = {'benimagacim','2'}; answer = inputdlg(prompt,name,numlines,defaultanswer); options.resize = 'on'; options.windowstyle = 'normal'; options.interpreter = 'tex'; q = get(s.pmh_window,'string'); error = 1; while error == 1 if ~any(strcmpi(answer{1},q)) error = 0; else answer(1) = inputdlg('type name used, enter another!') error = 1; end end p = get(s.pmh_window,{'string','value'}); q(length(q)+1) = answer(1); set(s.pmh_window,'string',q); function [ windowtypeinfo ] = windowtype( string ) if strcmpi(string,'type1') windowtypeinfo.typename = 'type1'; windowtypeinfo.length = 4; elseif strcmpi(string,'type2') windowtypeinfo.typename = 'type2'; windowtypeinfo.length = 5; end
the question isn't posed think understand you're asking. answer final thought, there better way store data. consider following simplified example:
function testgui % initialize gui h.myfig = figure; h.mybutton = uicontrol( ... 'style', 'pushbutton', ... 'units', 'normalized', ... 'position', [0.15 0.70, 0.70 0.15], ... 'string', 'add style' ... ); h.mymenu = uicontrol( ... 'style', 'popupmenu', ... 'units', 'normalized', ... 'position', [0.15 0.50 0.70 0.10], ... 'string', ' ' ... ); h.myedit = uicontrol( ... 'style', 'edit', ... 'units', 'normalized', ... 'position', [0.15 0.15 0.70 0.10], ... 'string', ' ' ... ); set(h.mybutton, 'callback',{@addwindow, h}); set(h.mymenu, 'callback',{@changewindow, h}); % initialize data structure mydata(1).typename = 'type1'; mydata(1).windowlength = 4; mydata(2).typename = 'type2'; mydata(2).windowlength = 5; updatepopup(mydata, h); % set initial editbox value set(h.myedit, 'string', mydata(1).windowlength); % store data later guidata(h.myfig, mydata); end function updatepopup(mydata, h) % update strings in popup menu based on data array typenames = {mydata.typename}; % collect existing typenames set(h.mymenu, 'string', typenames); % update popup menu end function addwindow(~, ~, h) % prompt user add window prompt = {'type name:','type length:'}; name = 'myprompt'; nlines = 1; defaultanswer = {'asdf', '1'}; myanswer = inputdlg(prompt, name, nlines, defaultanswer); % prepare new information storage newwindow.typename = myanswer{1}; newwindow.windowlength = str2double(myanswer{2}); mydata = guidata(h.myfig); % pull in existing data mydata = [mydata newwindow]; % add new data existing data updatepopup(mydata, h); % update popup menu guidata(h.myfig, mydata); % store data later end function changewindow(~, ~, h) % update editbox based on popup menu selection mydata = guidata(h.myfig); % pull in stored data windowselected = get(h.mymenu, 'value'); % index of window selected set(h.myedit, 'string', mydata(windowselected).windowlength); % change editbox end rather manually getting window type separate function (windowtype() in example), try storing data in gui data structure , pulling out necessary. allows use more generalized code , not have think how handle every case loop have now. easy follow , adapt needs.
Comments
Post a Comment