How do I assign the updated values in a UITable to MATLAB’s base workspace?
4 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Walter Roberson
on 16 Sep 2013
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 Roberson
on 16 Sep 2013
CellEditCallback is not invoked until the cell loses focus or return is pressed in the edit area.
More Answers (1)
Sean de Wolski
on 16 Sep 2013
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!