MATLAB: CellSelect​ionCallbac​k-update column name

app designercallbackcloumn name

Hello everyone, my goal is to get the column name "ColumnName" in the cell "text_area = uitextarea (….)" using the CellSelectionCallback function. However, clicking on the cell I do not get the update of the data.
I solved the problem by creating an "update" button, but I do not like how it works.
To find the "update" button:
– click on any cell of the table
– press "edit name column"
– by clicking on another cell and pressing the "update" button, it will change the name in the "text_area"
I would like to get the same result without the "update" button, but only by clicking on a table cell.
Below the code.
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Panel matlab.ui.container.Panel
addrowButton matlab.ui.control.Button
UITable matlab.ui.control.Table
addcolumnButton matlab.ui.control.Button
editnamecolumnButton matlab.ui.control.Button
end
properties (Access = private)
Data_temp=[] % Description
end
methods (Access = private)
function name_col(app,new_name) % new name column

app.UITable.ColumnName{(app.Data_temp(2))}=char(new_name.Value);
end
function column_name = old_name(app) % select old name column
column_name=app.UITable.ColumnName{(app.Data_temp(2))};
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.UITable.Data=cell(10,3); % default table
app.UITable.ColumnEditable=logical(1);
app.UITable.ColumnName ={'A','B','C'}; % default name
end
% Cell selection callback: UITable
function UITableCellSelection(app, event)
app.Data_temp=event.Indices; % salva gli indici nella proprietà Data_temp (creata dall'utente)UITableCellSelection(app);
end
% Button pushed function: addcolumnButton
function addcolumn_ButtonPushed(app, event)
app.UITable.Data(:,end+1)={''}; % add column
column_name=char(inputdlg('column name'));
app.UITable.ColumnName{end+1}=column_name;
end
% Button pushed function: addrowButton
function addrow_ButtonPushed(app, event)
app.UITable.Data(end+1,:)={''}; % add row below
end
% Button pushed function: editnamecolumnButton
function editnamecolumn_ButtonPushed(app, event)
if isempty(app.Data_temp)
warndlg('select a cell below the desidered column'); % warning: app.Data_temp is empty
else
column_name= old_name(app);
UI_Column=uifigure('Name','Column Name','Position',[100 100 429 276]);
text_area=uitextarea(UI_Column,'Position',[100 140 100 22],'Value', column_name);
new_name=uitextarea(UI_Column,'Position',[220 140 100 22]); % new name column
UI_label=uilabel(UI_Column,'Text','new column name','Position',[220 165 100 22]);
UI_label_2=uilabel(UI_Column,'Text','old column name','Position',[100 165 100 22]);
UI_btn=uibutton(UI_Column,'Text','update','ButtonPushedFcn',@(UI_btn,event) set(text_area,'Value',app.UITable.ColumnName{(app.Data_temp(2))})); % update selected column name
UI_save=uibutton(UI_Column,'Text','Save','Position',[220 100 100 22],'ButtonPushedFcn',@(UI_save,event) name_col(app,new_name)); % change and save new column name
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 697 560];
app.UIFigure.Name = 'UI Figure';
% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Position = [114 113 495 382];
% Create addrowButton
app.addrowButton = uibutton(app.Panel, 'push');
app.addrowButton.ButtonPushedFcn = createCallbackFcn(app, @addrow_ButtonPushed, true);
app.addrowButton.Position = [23 271 100 22];
app.addrowButton.Text = 'add row';
% Create UITable
app.UITable = uitable(app.Panel);
app.UITable.ColumnName = {'Column 1'; 'Column 2'};
app.UITable.RowName = {};
app.UITable.CellSelectionCallback = createCallbackFcn(app, @UITableCellSelection, true);
app.UITable.Position = [152 108 302 185];
% Create addcolumnButton
app.addcolumnButton = uibutton(app.Panel, 'push');
app.addcolumnButton.ButtonPushedFcn = createCallbackFcn(app, @addcolumn_ButtonPushed, true);
app.addcolumnButton.Position = [24 230 100 22];
app.addcolumnButton.Text = 'add column';
% Create editnamecolumnButton
app.editnamecolumnButton = uibutton(app.Panel, 'push');
app.editnamecolumnButton.ButtonPushedFcn = createCallbackFcn(app, @editnamecolumn_ButtonPushed, true);
app.editnamecolumnButton.Position = [19 189 112 22];
app.editnamecolumnButton.Text = 'edit name column';
end
end
methods (Access = public)
% Construct app
function app = app2
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Best Answer

Well, yes, for what you want to do to work, you need a way to update your UI_Column dialog whenever your CellSelectionCallback is called. At the moment, there's nothing that tells the dialog that the selection has changed.
One way to do that is to keep around the handle to the edit control in that dialog and update it in the CellSelectionCallback:
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Panel matlab.ui.container.Panel
addrowButton matlab.ui.control.Button
UITable matlab.ui.control.Table
addcolumnButton matlab.ui.control.Button
editnamecolumnButton matlab.ui.control.Button
end
properties (Access = private)
Data_temp=[] % Description
UI_ColumnOldName; % UItextarea for Selecting column name to edit
UI_ColumnNewName; % UItextarea for input of new column name
end
methods (Access = private)
function name_col(app,new_name) % new name column

app.UITable.ColumnName{(app.Data_temp(2))}=char(new_name.Value);
end
function SetSelectionName(app) % select old name column
if isgraphics(app.UI_ColumnOldName) %edit dialog is not live is the handle is not valid
app.UI_ColumnOldName.Value = app.UITable.ColumnName{(app.Data_temp(2))};
app.UI_ColumnNewName.Value = app.UI_ColumnOldName.Value;
end
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.UITable.Data=cell(10,3); % default table
app.UITable.ColumnEditable=true;
app.UITable.ColumnName ={'A','B','C'}; % default name
end
% Cell selection callback: UITablea
function UITableCellSelection(app, event)
app.Data_temp=event.Indices; % salva gli indici nella proprietà Data_temp (creata dall'utente)UITableCellSelection(app);
app.SetSelectionName;
end
% Button pushed function: addcolumnButton
function addcolumn_ButtonPushed(app, ~)
app.UITable.Data(:,end+1)={''}; % add column
column_name=char(inputdlg('column name'));
app.UITable.ColumnName{end+1}=column_name;
end
% Button pushed function: addrowButton
function addrow_ButtonPushed(app, ~)
app.UITable.Data(end+1,:)={''}; % add row below
end
% Button pushed function: editnamecolumnButton
function editnamecolumn_ButtonPushed(app, ~)
if isempty(app.Data_temp)
warndlg('select a cell below the desidered column'); % warning: app.Data_temp is empty
else
UI_Column=uifigure('Name','Column Name','Position',[100 100 429 276]);
app.UI_ColumnOldName=uitextarea(UI_Column,'Position',[100 140 100 22]);
app.UI_ColumnNewName=uitextarea(UI_Column,'Position',[220 140 100 22]); % new name column
UI_label=uilabel(UI_Column,'Text','new column name','Position',[220 165 100 22]);
UI_label_2=uilabel(UI_Column,'Text','old column name','Position',[100 165 100 22]);
UI_save=uibutton(UI_Column,'Text','Save','Position',[220 100 100 22],'ButtonPushedFcn',@(UI_save,event) name_col(app,new_name)); % change and save new column name
app.SetSelectionName; %update textarea controls
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 697 560];
app.UIFigure.Name = 'UI Figure';
% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Position = [114 113 495 382];
% Create addrowButton
app.addrowButton = uibutton(app.Panel, 'push');
app.addrowButton.ButtonPushedFcn = createCallbackFcn(app, @addrow_ButtonPushed, true);
app.addrowButton.Position = [23 271 100 22];
app.addrowButton.Text = 'add row';
% Create UITable
app.UITable = uitable(app.Panel);
app.UITable.ColumnName = {'Column 1'; 'Column 2'};
app.UITable.RowName = {};
app.UITable.CellSelectionCallback = createCallbackFcn(app, @UITableCellSelection, true);
app.UITable.Position = [152 108 302 185];
% Create addcolumnButton
app.addcolumnButton = uibutton(app.Panel, 'push');
app.addcolumnButton.ButtonPushedFcn = createCallbackFcn(app, @addcolumn_ButtonPushed, true);
app.addcolumnButton.Position = [24 230 100 22];
app.addcolumnButton.Text = 'add column';
% Create editnamecolumnButton
app.editnamecolumnButton = uibutton(app.Panel, 'push');
app.editnamecolumnButton.ButtonPushedFcn = createCallbackFcn(app, @editnamecolumn_ButtonPushed, true);
app.editnamecolumnButton.Position = [19 189 112 22];
app.editnamecolumnButton.Text = 'edit name column';
end
end
methods (Access = public)
% Construct app
function app = app2
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end