saving user input from uitable matlab GUI?

richyo1000 picture richyo1000 · Jun 26, 2013 · Viewed 7.3k times · Source

I'm creating a GUI (not using GUIDE) I'm looking for a convenient way for a user to enter some data. I figured uitable would be ideal except i can't seem to figure out how to store the user input from the table. I'd rather not use the celleditcallback function - ideally i'd like to save out the data all at once at the end using a save button or similar, any ideas? code for the table (this is within it's own function):

dat =  {0,  0,  0, true;...
        0,  0,  0, true;...   
        0,  0,  0, true;};
columnname =   {'x-pos', 'y-pos', 'dimns', 'Include?'};
NC = numel(columnname);
rowNumber = zeros(NR,NC);
columnformat = {'numeric', 'numeric', 'numeric','logical'};
columneditable =  [true true true true true];
rowname = {'Dat1','Dat2','Dat3'};
Config = uitable('Units','normalized','Position',[0 0 0.2 0.4],...
            'Data', dat,... 
            'ColumnName', columnname,...
            'ColumnFormat', columnformat,...
            'ColumnEditable', columneditable,...
            'RowName',rowname);
cell2mat(dat(:,1:3));
gh =get(Config,'Data');

Thanks in advance for any advice

Answer

Shaun314 picture Shaun314 · Jun 26, 2013

I think the big thing is that you need a waitfor(gcf) at the end of your function and before you assign the table data to the output.

Check out this example:

function [out1]=myGUIwithATable(inputs..)

myTable=uitable(.......)

waitfor(gcf) 

%This command will wait until you close the GUI before doing the code after 
% it. We use this to allow you to enter all your data and whatnot, then once  
% you close the fig, it will execute your save commands

out1=get(myTable,'Data');

So that ^^^ is how you can assign output variables to your table values

Saving via button is very very easy. On your button callback, just do

save('fileName.mat',get(myTable,'Data'))

Hope that helps!