matlab - Programmatically save changes of an editable uitable -
i created uitable in matlab fill various values , options. looks like:

the corresponding code following:
selector_1 = { 'a'; 'b' ; 'c' }; selector_2 = { 'a.1'; 'a.2'; 'a.3'; ... 'b.1'; 'b.2'; 'b.3'; ... 'c.1'; 'c.2'; 'c.3' }; rows = 5; f = figure('name','configuration of output','position',[200 200 430 25+rows*20],'numbertitle','off','menubar','none'); dat = {'select outputfile...', 'select identifier...', 'specifier', 'index'}; dat = repmat(dat,rows,1); columnname = {'output file ',... 'identifier ',... 'specifier ', 'index'}; columnformat = { {selector_1{:}}, {selector_2{:}}, 'char', 'numeric' }; columneditable = [true true true true]; t = uitable('units','normalized','position',... [0 0 1 1], 'data', dat,... 'columnname', columnname,... 'columnformat', columnformat,... 'columneditable', columneditable,... 'rowname',[]); set(t, 'data', dat,'celleditcallback','get(t,''data'')'); so run code , figure open. underlying script has therefore finished. when edit table uitable object changed , after finished can final configuration with:
finalconfig = get(t,'data'); but thing need manually type line, because script has finished. if put line @ end of script, error.
so thought using following loop, detect when close table , store last configuration
while ~isempty(findobj('name','configuration of output')) % action end finalconfig = get(t,'data'); and tried put inside loop, whole script, set command including celleditcallback, , other things, nothing worked. either script stucked inside loop or display of table not updated when edit value. tried drawnow @ different positions. how 1 handles situation? how can automatically store final results? assume "closing window" best action detect, don't think implement "save" button. tried create gui using guide got lost, hope solve without.
edit: able implement "save"-button , tried callback follows:
uimenu('label','save configuration','callback',@saveconfig); function saveconfig(~,~) output = get(t,'data',); save([pwd 'output.mat'],'output'); end also implemented custom closerequestfcn suggested lucius domitius ahenobarbus. have either 1 of following problems:
1) define script, works fine, need define functions @saveconfig (actually favorite) or @my_closefcn unique function-file in workspace , have hard time pass right parameters dat remains same, though gets changend. (the example mathworks site works! doesn't need additional parameters.)
2) when use
function configuration % script above end i can implement @saveconfig or @my_closefcn directly (nested) , guess passing of parameters work fine. editing of table not work anymore, throwing following error:
error using handle.handle/get
invalid or deleted object.
error while evaluating uitable celleditcallback
how solve that? know can add buttons uitable avoid guide. code above executable, i'd glad if try see actual problem is, hard describe.
depending on using guide or not:
use closerequestfcn->
without guide use:
%write own closerequestfcn , set figure closerequest-callback it: set(gcf,'closerequestfcn',@my_closefcn) %use gcf or handle of figure directly and define my_closefcn including delete statement figure-handle, else figure not close :)
see docs more information "redefining closerequestfcn".
with guide:
you can edit closerequestfcn inspecting figure. there field called closerequestfcn create function automatically , dont need take care getting handle. this:
function figure1_closerequestfcn(hobject, eventdata, handles) % hobject handle figure1 (see gcbo) % eventdata reserved - defined in future version of matlab % handles structure handles , user data (see guidata) % hint: delete(hobject) closes figure delete(hobject); now before deleting figure, should able data of uitable (if have handle) , suggest assign data base workspace, like:
assignin('base', 'finaltabledata', get(mytablehandle,'data')); edit
as not clear enough, see example:
(use within 1 single script)->
function test h=figure; x=1:10; mytable=uitable(h,'data',x); set(h,'closerequestfcn',@myclosefcn) %give unique tag: set(h,'tag', 'mytag') set(mytable,'tag','mytabletag') end function myclosefcn(~,~) myfigure=findobj('tag','mytag'); mydata=get(findobj(myfigure,'tag','mytabletag'),'data') assignin('base','mytestdata',mydata) delete(myfigure) end in fact, there no need take care parameters of closereq-callback, if know how find handle of figure! give figure/uitable able identify later on. used 'tag', because first thing think of, there other parameters well.
Comments
Post a Comment