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

4 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de Sept. de 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 comentarios
Walter Roberson
Walter Roberson el 16 de Sept. de 2013
CellEditCallback is not invoked until the cell loses focus or return is pressed in the edit area.
Brad
Brad el 17 de Sept. de 2013
Editada: Brad el 17 de Sept. de 2013
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.

Iniciar sesión para comentar.

Más respuestas (1)

Sean de Wolski
Sean de Wolski el 16 de Sept. de 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
  1 comentario
Brad
Brad el 16 de Sept. de 2013
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.

Iniciar sesión para comentar.

Categorías

Más información sobre App Building en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by