How do I assign the updated values in a UITable to MATLAB’s base workspace?

So after some research, and advice from Arthur, I’ve decided to take an alternate approach to creating a GUI containing a combination of static/editable text boxes (about 80 of them).
I’ve started to build this GUI by using the following code:
%function UITable
f = figure('Position',[100 100 300 300]);
rowname = {'Message Block';
'Word 1';
'Word 2';
'Word 3';
'Word 4';
'Word 5';
'Word 6';
'Word 7';
'Word 8';
'Word 9';
'Word 10'};
columnname = {'Value'};
columnformat = {'numeric'};
columneditable = [true];
columnwidth = {'auto'};
DefaultData = {' '; 0.12345; 1.2345; 12.345; 21102; 0.12345; 1.2345; 12.345; 21102; 0.12345; 1.2345};
t = uitable('Units','normalized','Position',...
[0.1 0.1 0.9 0.9], 'Data', DefaultData,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable,...
'ColumnWidth', columnwidth,...
'RowName',rowname);
When the figure opens, I see what I’m expecting, and I can now edit each of the 11 default values. But what I can’t figure out is how to assign the updated values to MATLAB’s base workspace.
I’ve seen this done with values that are NOT updated.
Can the same be done with user updated values?
Thanks.

 Accepted Answer

Set a CellEditCallback for the uitable. In that callback, you can do things like
currentvals = get(hObject, 'Data');
assignin('main', 'message_block', currentvals{1});
assignin('main', 'word_vals', cell2mat(currentvals(2:end)) );

4 Comments

Walter, this is the first time I've used a celleditcallback. So using the example at http://www.mathworks.com/products/matlab/examples.html?file=/products/demos/shipping/matlab/uitabledemo.html#4, I can see where the functionality of the callback comes in play.
But what I don't understand is how the assignin function is used to get the updated values. Using the above as an example, wouldn't I just add an assignin function to the callback? Something like: assignin('base', 'tableData', data);
Walter, I found and fixed the first problem in my celleditcallback.
function [] = ValueChange(varargin)
h = varargin{1};
currentvals = get(h, 'Data');
assignin('base', 'Message_BlockID', currentvals{1});
assignin('base', 'Updated_Values', cell2mat(currentvals(2:end)) );
end
The 2nd problem is proving to be even more fun. The figure opens up and I edit the default values as needed. However, the Message_BlockID and Updated_Values variables don't immediately populate the base workspace. If I click on the Edit drop down menu in the figure, they are then populated. And If I make subsequent changes, the variables update when I click on the Edit drop down menu, again.
I haven't come across this behavior before. Could I be missing a command required to make the variables auto-populate with clicking on the Edit button within the figure?
CellEditCallback is not invoked until the cell loses focus or return is pressed in the edit area.
This is strange. I can execute this code on a PC running MATLAB 2010A and it runs as expected. But on a PC running 2012B, the values don't update when return is pressed within the edit area.

Sign in to comment.

More Answers (1)

Hi Brad,
I would take an OOP approach here. For example:
h = uitableData(magic(5))
h.data
Now make some changes in the table:
h.data
So rather than having it randomly create a variable in the worksapce, h.data will always be the current value of the uitable.
And the example class:
classdef uitableData < handle
%Data, we want this accessible from base
properties
data
end
%Handle to uitable
properties(GetAccess=protected,SetAccess=protected)
hT
end
%Constructor, use it like any other function
methods
function obj = uitableData(data)
%You could have this function accept data
obj.data = data;
figure;
obj.hT = uitable('Data',data,...
'CellEditCallback',@(src,evt)obj.updateData(src,evt),...
'ColumnEditable',true);
end
end
%User won't call this explicitly
methods(Access=protected)
function updateData(obj,~,evt)
%Store the new data
obj.data(evt.Indices(1),evt.Indices(2)) = evt.NewData;
%EditData, or if you want to enforce that the new data meets
%some constraint you could assert that EditData is good and if
%not use PreviouData
end
end
end

1 Comment

Sean, I gotta be honest with you: I haven't done hardly any OOP. I appreciate the inputs and help, but I beter keep this bare bones simple if I want to finish it.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!