UIWAIT CellSelectionCallback and sum elements

1 view (last 30 days)
Hello everyone, my goal is to make the sum of the elements of a column. By activating the "sum col el" button (sum column elements) and selecting the desired column, I get the desired result. However, in order to obtain the same result, the "sum with el" button must be turned off and the procedure repeated. What I would like to get:
- activate the button once and add the elements of a column without repeating the procedure.
classdef app2_dinamico < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
TABELLAPanel matlab.ui.container.Panel
addrowButton matlab.ui.control.Button
UITable matlab.ui.control.Table
addcolumnButton matlab.ui.control.Button
editnamecolumnButton matlab.ui.control.Button
deleterowButton matlab.ui.control.Button
deletecolumnButton matlab.ui.control.Button
ResetButton matlab.ui.control.Button
SaveButton matlab.ui.control.Button
LoaddataButton matlab.ui.control.Button
SumColElButton matlab.ui.control.StateButton
end
properties (Access = private)
Data_temp=[]; % Description
UI_ColumnOldName; % proprietà per uitextarea
UI_ColumnNewName; % proprietà per uitextarea
end
methods (Access = private)
function name_col(app) % new name column (assegna il nuovo nome alla colonna)
app.UITable.ColumnName{(app.Data_temp(2))}=char(app.UI_ColumnNewName.Value);
end
% function column_name = old_name(app) % select old name column
% column_name=app.UITable.ColumnName{(app.Data_temp(2))};
% end
function SetSelectionName(app) % select old name column
if isgraphics(app.UI_ColumnOldName) % se la finestra di dialogo UI_ColumnOldName non è stata attivata la funzione UITableCellSelection(app, event)
% non esegue SetSelectionName
app.UI_ColumnOldName.Value=app.UITable.ColumnName{(app.Data_temp(2))};
% app.UI_ColumnNewName.Value=app.UI_ColumnOldName.Value;
end
end
function UI_resume(app,event)
if app.SumColElButton.Value==0
return
elseif app.SumColElButton.Value==1 && length(app.Data_temp(:,1))>1
uiresume(app.UIFigure);
else
return
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=logical(1);
app.UITable.ColumnName ={'A','B','C'}; % default name
% load('Dati.mat','app1')
% app.UITable.Data(:,:)=app1.UITable.Data
end
% Button pushed function: LoaddataButton
function LoaddataButtonPushed(app, event)
[filename]=uigetfile({'*.mat'},'Select Mat-file');
if filename~=0;
load(filename,'app1');
app.UITable.Data(:,:)=app1.UITable.Data;
app.UITable.ColumnName=app1.UITable.ColumnName;
else
return
end
% load('Dati.mat','app1')
% app.UITable.Data(:,:)=app1.UITable.Data
end
% Button pushed function: ResetButton
function ResetButtonPushed(app, event)
app.UITable.Data(:,:)={[]};
end
% Button pushed function: SaveButton
function SaveButtonPushed(app, event)
app1=app;
[filename, pathname]=uiputfile('*.mat','Save data');
if filename~=0;
newfilename = fullfile(pathname, filename); % resistuisce la path con nome file
save(newfilename,'app1');
else
return
end
app.UIFigure.Name=newfilename;
app.TABELLAPanel.Title=newfilename(1:end-4);
end
% Value changed function: SumColElButton
function SumColElButtonValueChanged(app, event)
value_Button = app.SumColElButton.Value
b_temp=app.Data_temp(:,:);
if numel(b_temp)>2 || isempty(b_temp)
warndlg('disable SUM COL. EL. button and select a single cell. Then activate it')
return
end
if value_Button==1
if isrow(b_temp)
uiwait(app.UIFigure);
app.UITable.Data(b_temp(1),b_temp(2))={sum(str2double(app.UITable.Data(app.Data_temp(:,1),app.Data_temp(1,2))),1)};
else
return
end
elseif value_Button==0
return
end
end
% Cell edit callback: UITable
function UITableCellEdit(app, event)
cell_edit=char(app.UITable.Data(app.Data_temp(1),app.Data_temp(2)));
if cell_edit(1)=='='
app.UITable.Data(app.Data_temp(1),app.Data_temp(2))=num2cell(eval(cell_edit(2:end)));
else
return
end
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);
app.SetSelectionName;
app.UI_resume;
% app.Data_temp(:,2) % prende tutti gli indice colonna
% app.Data_temp(:,1) % prende tutti gli indici di riga selezionati
% app.Data_temp(1,2)
% app.UITable.Data(app.Data_temp(:,1),app.Data_temp(:,2))
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: deletecolumnButton
function deletecolumnButtonPushed(app, event)
if isrow(app.Data_temp(:,:)) % verifica se il vettore è una riga (indice di riga e colonna). Selezionando più celle si ha un matrice
app.UITable.Data(:,app.Data_temp(2))=[]; % delete row
app.UITable.ColumnName(app.Data_temp(2))=[];
else
return
end
end
% Button pushed function: deleterowButton
function deleterowButtonPushed(app, event)
if isrow(app.Data_temp(:,:))
app.UITable.Data(app.Data_temp(1),:)=[]; % delete row
else
return
end
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
app.UI_ColumnOldName=uitextarea(UI_Column,'Position',[100 140 100 22]);
app.UI_ColumnNewName=uitextarea(UI_Column,'Position',[220 140 100 22]);
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)); % change and save new column name
app.SetSelectionName;
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 938 545];
app.UIFigure.Name = 'UI Figure';
% Create TABELLAPanel
app.TABELLAPanel = uipanel(app.UIFigure);
app.TABELLAPanel.TitlePosition = 'centertop';
app.TABELLAPanel.Title = ' TABELLA';
app.TABELLAPanel.Position = [23 52 901 476];
% Create addrowButton
app.addrowButton = uibutton(app.TABELLAPanel, 'push');
app.addrowButton.ButtonPushedFcn = createCallbackFcn(app, @addrow_ButtonPushed, true);
app.addrowButton.Position = [16 391 100 22];
app.addrowButton.Text = 'add row';
% Create UITable
app.UITable = uitable(app.TABELLAPanel);
app.UITable.ColumnName = {'Column 1'; 'Column 2'};
app.UITable.RowName = {};
app.UITable.CellEditCallback = createCallbackFcn(app, @UITableCellEdit, true);
app.UITable.CellSelectionCallback = createCallbackFcn(app, @UITableCellSelection, true);
app.UITable.Position = [134 24 748 389];
% Create addcolumnButton
app.addcolumnButton = uibutton(app.TABELLAPanel, 'push');
app.addcolumnButton.ButtonPushedFcn = createCallbackFcn(app, @addcolumn_ButtonPushed, true);
app.addcolumnButton.Position = [17 350 100 22];
app.addcolumnButton.Text = 'add column';
% Create editnamecolumnButton
app.editnamecolumnButton = uibutton(app.TABELLAPanel, 'push');
app.editnamecolumnButton.ButtonPushedFcn = createCallbackFcn(app, @editnamecolumn_ButtonPushed, true);
app.editnamecolumnButton.Position = [10 221 112 22];
app.editnamecolumnButton.Text = 'edit name column';
% Create deleterowButton
app.deleterowButton = uibutton(app.TABELLAPanel, 'push');
app.deleterowButton.ButtonPushedFcn = createCallbackFcn(app, @deleterowButtonPushed, true);
app.deleterowButton.Position = [17 310 100 22];
app.deleterowButton.Text = 'delete row';
% Create deletecolumnButton
app.deletecolumnButton = uibutton(app.TABELLAPanel, 'push');
app.deletecolumnButton.ButtonPushedFcn = createCallbackFcn(app, @deletecolumnButtonPushed, true);
app.deletecolumnButton.Position = [18 270 100 22];
app.deletecolumnButton.Text = 'delete column';
% Create ResetButton
app.ResetButton = uibutton(app.TABELLAPanel, 'push');
app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
app.ResetButton.Position = [18 107 100 22];
app.ResetButton.Text = 'Reset';
% Create SaveButton
app.SaveButton = uibutton(app.TABELLAPanel, 'push');
app.SaveButton.ButtonPushedFcn = createCallbackFcn(app, @SaveButtonPushed, true);
app.SaveButton.Position = [19 70 100 22];
app.SaveButton.Text = 'Save';
% Create LoaddataButton
app.LoaddataButton = uibutton(app.TABELLAPanel, 'push');
app.LoaddataButton.ButtonPushedFcn = createCallbackFcn(app, @LoaddataButtonPushed, true);
app.LoaddataButton.Position = [19 40 100 22];
app.LoaddataButton.Text = 'Load data';
% Create SumColElButton
app.SumColElButton = uibutton(app.TABELLAPanel, 'state');
app.SumColElButton.ValueChangedFcn = createCallbackFcn(app, @SumColElButtonValueChanged, true);
app.SumColElButton.Text = 'Sum Col. El.';
app.SumColElButton.BackgroundColor = [0.9412 0.9412 0.9412];
app.SumColElButton.FontWeight = 'bold';
app.SumColElButton.Position = [18 165 100 22];
end
end
methods (Access = public)
% Construct app
function app = app2_dinamico
% 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
  1 Comment
Alok Nimrani
Alok Nimrani on 4 Jan 2019
Hi Dario,
Your current workflow requires 'SumColElButtonValueChanged' event to occur to perform the column selection and once you are done with the selection, doing another selection is not possible until the same event occurs again. You might have to change your logic to achieve the goal of multiple selections. You can consider using an extra event to enable multiple column selections.
Hope this helps.

Sign in to comment.

Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!