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

조회 수: 1 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2013년 9월 16일
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
Walter Roberson
Walter Roberson 2013년 9월 16일
CellEditCallback is not invoked until the cell loses focus or return is pressed in the edit area.
Brad
Brad 2013년 9월 17일
편집: Brad 2013년 9월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2013년 9월 16일
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
Brad
Brad 2013년 9월 16일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by