Save data in cell using get function

Hi everyone, I've some issue in saving some tables I create in App Designer. The app doesn't have any uitab or uitable(parent of uitab), but they're created at the startup of the app by defining their properties. Their multiplicity depends on the file .mat from which data are loaded. Table 1 is stored in t{1}, table 2 in t{2} and so on. I don't really know if this is a smart way to reach my goal, but it makes me quite flexible in the management of the new tables are added within the app and leaving the buttons functions work. By the way, whenever I try to save the tables (eventually more than the previous number of tables), only the last table saved contains the data, whereas the previouses are shown as empty arrays. I tried several methods in order to store data from this kind of variable, but the function "get" seems the only one able to do it unless this inconvienece.
Thanks everybody who could help me.
function startupFcn(app)
% Create TabGroup
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [16 248 820 220];
% Load file
loadfile = load('path\score_data.mat');
% Create tabs cell
app.tabs = cell(1,length(loadfile));
for i=1:length(loadfile)
app.tabs{i} = uitab(app.TabGroup,'Title',['Semester ' num2str(i)]);
end
% Add tables
app.tables = cell(1,length(loadfile));
for i=1:length(loadfile)
uit = uitable('Parent',app.tabs{i},'CellEditCallback',@(event,lbl)cellchanged(app));
uit.ColumnWidth = {'auto',125,125,100};
uit.ColumnName = {'Subject','Uniwhere Average','My Score','Weight'};
uit.ColumnEditable = true;
uit.RowName = {};
uit.Position = [15,15,790,170];
app.tables{i} = uit;
app.tables{i}.Data = loadfile.t{i};
end
end
...
function SaveTableScoresButtonPushed(app, event)
t = cell(1,length(app.tables));
t{1} = get(app.tables{1},'Data');
t{2} = get(app.tables{2},'Data');
% for i=1:length(app.tables)
% t{i} = get(app.tables{i},'Data');
% end
save('path\score_data.mat',"t")
end

Answers (1)

I'm not sure, if I understand, what you are asking for.
This part of the code looks strange:
app.tables = cell(1, length(loadfile));
for i = 1:length(loadfile)
...
app.tables{i}.Data = loadfile.t{i};
end
It looks like loadfile is a scalar and the actual data are stored in the array loadfile.t. So maybe you mean:
app.tables = cell(1, length(loadfile.t));
% ^^
for i = 1:length(loadfile.t)
% ^^
...
app.tables{i}.Data = loadfile.t{i};
end

3 Comments

Hi, first of all thanks for your suggestion. It surely makes the code better.
Well, the issue is related to the fact that within the app is possible to increase the number of tables{i} and edit the Data by adding rows. In order to save these data, I inserted a button at this purpose. I'd want to make the function save the data of app.tables{i} in cell t{i}. I chose the function "get" because is the only one able to deal with without errors. The issue is that in the cell "t" happens to save only the data of app.tables{i} in t{i}, whereas all the previous cells are empty.
No difference in changing the number of tables: it always saves the last one. No difference in using a struct. I attach the file if it could ease the troubleshooting. Thanks.
"The issue is that in the cell "t" happens to save only the data of app.tables{i} in t{i}, whereas all the previous cells are empty." - what is "i" in this statement?
Which code are you using? The code you have posted contains an access of "t{i}" only in the comments.
Your mlapp does not open in my Matlab version. Prefer to post the code as text instead.
Sorry if i didn't explain well, I'll try better.
At first instance, t is the name of the cell where an empty table is stored in "score_data.mat". Then, the table is loaded in the app and changes can be applied. No problem if the cell "t" contains only one table (i.e. if there is only one tab in the app). This "t" is the one of "loadfile.t". Because I want to load the saved file at every startup, I have to save the tables in a cell called "t" and overwrite what's in "score_data.mat". "i" is given by the number of tables currently created by the app for which exists one table with eventually some data to be saved in "score_data.mat". It follows the full code written on App Designer so with some automatically-created lines. Let me know if you check whatever error. Thank you!
P.S. Remember to change the path of the file "score_data.mat".
classdef app_score_v4 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
AddSemesterButton matlab.ui.control.Button
TotalCFUEditField matlab.ui.control.NumericEditField
TotalCFUEditFieldLabel matlab.ui.control.Label
ThesismaxscoreEditField matlab.ui.control.NumericEditField
ThesismaxscoreEditFieldLabel matlab.ui.control.Label
SaveTableScoresButton matlab.ui.control.Button
AddSubjectButton matlab.ui.control.Button
AveragetoobtainfromnowEditField matlab.ui.control.NumericEditField
AveragetoobtainfromnowLabel matlab.ui.control.Label
MaxscoreachievableEditField matlab.ui.control.NumericEditField
MaxscoreachievableEditFieldLabel matlab.ui.control.Label
CurrentAverageEditField matlab.ui.control.NumericEditField
CanteditLabel matlab.ui.control.Label
MaxscoregoalEditField matlab.ui.control.NumericEditField
MaxscoregoalEditFieldLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
properties (Access = public)
Index % Description
end
properties (Access = private)
tabs % property for add tab button
tables % property of table automatically added
TabGroup
j
end
methods (Access = private)
function cellchanged(app)
CurrentAverageEditFieldValueChanged(app)
MaxscoreachievableEditFieldValueChanged(app)
AveragetoobtainfromnowEditFieldValueChanged(app)
UIAxesButtonDown(app)
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% Create TabGroup
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [16 248 820 220];
% Load file
loadfile = load('directory\score_data_empty.mat'); % update with your directory
% Create tabs cell
app.tabs = cell(1,length(loadfile));
for i=1:length(loadfile)
app.tabs{i} = uitab(app.TabGroup,'Title',['Semester ' num2str(i)]);
end
% Add tables
app.tables = cell(1,length(loadfile.t));
for i=1:length(loadfile.t)
uit = uitable('Parent',app.tabs{i},'CellEditCallback',@(event,lbl)cellchanged(app));
uit.ColumnWidth = {'auto',125,125,100};
uit.ColumnName = {'Subject','Uniwhere Average','My Score','Weight'};
uit.ColumnEditable = true;
uit.RowName = {};
uit.Position = [15,15,790,170];
app.tables{i} = uit;
app.tables{i}.Data = loadfile.t{i};
end
% Current Average
CurrentAverageEditFieldValueChanged(app)
% Max score
MaxscoreachievableEditFieldValueChanged(app)
% Average to obtain from now
AveragetoobtainfromnowEditFieldValueChanged(app)
% Plot
UIAxesButtonDown(app)
end
% Button pushed function: SaveTableScoresButton
function SaveTableScoresButtonPushed(app, event)
t = cell(1,length(app.tables));
t{1} = get(app.tables{1},'Data');
t{2} = get(app.tables{2},'Data');
% Generalisation for whatever number of tables
% for i=1:length(app.tables)
% t{i} = get(app.tables{i},'Data');
% end
save('directory\score_data_empty.mat','t'); % update with your directory
end
% Button pushed function: AddSubjectButton
function AddSubjectButtonPushed(app, event)
selectedTab = app.TabGroup.SelectedTab;
h = (selectedTab == app.TabGroup.Children); % logic array
h = find(h); % find the position of the selected tab
rowdata = [{'Subject'}, 0, 0, 0];
app.tables{h}.Data = [app.tables{h}.Data; rowdata];
end
% Value changed function: CurrentAverageEditField
function CurrentAverageEditFieldValueChanged(app, event)
tt = [];
for i = 1:length(app.tables)
tt = [tt; table2array(app.tables{i}.Data(:,3:4))];
end
x = (tt(:,2) ~= 0); % logical array = 1 for weight not equal 0
tt = tt(x,:); % eliminate all the rows with null weight
num = (tt(:,1))' * tt(:,2);
den = sum(tt(:,2));
average = num/den;
app.CurrentAverageEditField.Value = average;
end
% Value changed function: MaxscoreachievableEditField
function MaxscoreachievableEditFieldValueChanged(app, event)
average = app.CurrentAverageEditField.Value;
max = average/30*110 + app.ThesismaxscoreEditField.Value;
app.MaxscoreachievableEditField.Value = max;
end
% Value changed function: MaxscoregoalEditField
function MaxscoregoalEditFieldValueChanged(app, event)
% Average to obtain
AveragetoobtainfromnowEditFieldValueChanged(app)
end
% Value changed function: ThesismaxscoreEditField
function ThesismaxscoreEditFieldValueChanged(app, event)
% Max score
MaxscoreachievableEditFieldValueChanged(app)
% Average to obtain
AveragetoobtainfromnowEditFieldValueChanged(app)
end
% Value changed function: AveragetoobtainfromnowEditField
function AveragetoobtainfromnowEditFieldValueChanged(app, event)
tot_cfu = app.TotalCFUEditField.Value;
tt = [];
for i = 1:length(app.tables)
tt = [tt; table2array(app.tables{i}.Data(:,3:4))];
end
x = (tt(:,2) ~= 0); % logical array = 1 for weight not equal 0
tt = tt(x,:); % eliminate all the rows with null weight
current_av = app.CurrentAverageEditField.Value;
goal_av = (app.MaxscoregoalEditField.Value - app.ThesismaxscoreEditField.Value)/110*30;
current_cfu = sum(tt(:,2));
last_cfu = tot_cfu - current_cfu;
to_obtain = (goal_av*tot_cfu - current_av*current_cfu) / last_cfu;
app.AveragetoobtainfromnowEditField.Value = to_obtain;
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
cla(app.UIAxes);
% Current average
curr_av = app.CurrentAverageEditField.Value;
% Marks
tt = [];
for i = 1:length(app.tables)
tt = [tt; table2array(app.tables{i}.Data(:,3:4))];
end
x = (tt(:,2) ~= 0); % logical array = 1 for weight not equal 0
tt = tt(x,:); % eliminate all the rows with null weight
marks = tt(:,1);
% Instant average
instant_av = zeros(1,length(tt));
num = 0;
for i=1:length(tt)
num = num + tt(i,1) * tt(i,2);
cfu = sum(tt(1:i,2));
instant_av(i) = num/cfu;
end
% x vectors
x = 1:length(tt);
curr_av = curr_av * ones(length(tt),1);
% Smooth curve
samplingRateIncrease = 10;
newXSamplePoints = linspace(min(x), max(x), length(x) * samplingRateIncrease);
smoothed_marks = spline(x, marks, newXSamplePoints);
smoothed_inst_av = spline(x, instant_av, newXSamplePoints);
plot(app.UIAxes,newXSamplePoints,smoothed_marks,Color='#0072BD')
hold(app.UIAxes,"on")
plot(app.UIAxes,x,curr_av,Color='#D95319')
plot(app.UIAxes,newXSamplePoints,smoothed_inst_av,Color='#EDB120')
set(app.UIAxes,'XTickLabel',[]);
legend(app.UIAxes,'Marks', 'Current average', 'Instant average',Location='northwest');
end
% Value changed function: TotalCFUEditField
function TotalCFUEditFieldValueChanged(app, event)
AveragetoobtainfromnowEditFieldValueChanged(app)
end
% Button pushed function: AddSemesterButton
function AddSemesterButtonPushed(app, event)
% Consider a counter in order to manage the further tabs added later
app.j = length(app.tables)+1;
i = app.j; % change name to simplify readability
% Set up the tabs cell
app.tabs = cell(1,i);
% Create tabs
app.tabs{i} = uitab(app.TabGroup,'Title',['Semester ' num2str(i)]);
% Add table
app.tables = cell(1,i);
uit = uitable('Parent',app.tabs{i},'CellEditCallback',@(event,lbl)cellchanged(app));
uit.ColumnWidth = {'auto',125,125,100};
uit.ColumnName = {'Subject','Uniwhere Average','My Score','Weight'};
uit.ColumnEditable = true;
uit.RowName = {};
uit.Position = [15,15,790,170];
app.tables{i} = uit;
app.tables{i}.Data = [{'Subject'}, 0, 0, 0];
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 850 480];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.Scrollable = 'on';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Scores')
ylabel(app.UIAxes, 'Score')
zlabel(app.UIAxes, 'Z')
app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
app.UIAxes.Position = [415 1 436 237];
% Create MaxscoregoalEditFieldLabel
app.MaxscoregoalEditFieldLabel = uilabel(app.UIFigure);
app.MaxscoregoalEditFieldLabel.HorizontalAlignment = 'center';
app.MaxscoregoalEditFieldLabel.Position = [240 85 87 22];
app.MaxscoregoalEditFieldLabel.Text = 'Max score goal';
% Create MaxscoregoalEditField
app.MaxscoregoalEditField = uieditfield(app.UIFigure, 'numeric');
app.MaxscoregoalEditField.Limits = [66 115];
app.MaxscoregoalEditField.ValueChangedFcn = createCallbackFcn(app, @MaxscoregoalEditFieldValueChanged, true);
app.MaxscoregoalEditField.Position = [339 85 55 22];
app.MaxscoregoalEditField.Value = 110;
% Create CanteditLabel
app.CanteditLabel = uilabel(app.UIFigure);
app.CanteditLabel.HorizontalAlignment = 'right';
app.CanteditLabel.Position = [30 135 92 22];
app.CanteditLabel.Text = 'Current Average';
% Create CurrentAverageEditField
app.CurrentAverageEditField = uieditfield(app.UIFigure, 'numeric');
app.CurrentAverageEditField.ValueChangedFcn = createCallbackFcn(app, @CurrentAverageEditFieldValueChanged, true);
app.CurrentAverageEditField.Editable = 'off';
app.CurrentAverageEditField.Position = [139 135 55 22];
% Create MaxscoreachievableEditFieldLabel
app.MaxscoreachievableEditFieldLabel = uilabel(app.UIFigure);
app.MaxscoreachievableEditFieldLabel.HorizontalAlignment = 'right';
app.MaxscoreachievableEditFieldLabel.Position = [210 135 122 22];
app.MaxscoreachievableEditFieldLabel.Text = 'Max score achievable';
% Create MaxscoreachievableEditField
app.MaxscoreachievableEditField = uieditfield(app.UIFigure, 'numeric');
app.MaxscoreachievableEditField.ValueChangedFcn = createCallbackFcn(app, @MaxscoreachievableEditFieldValueChanged, true);
app.MaxscoreachievableEditField.Editable = 'off';
app.MaxscoreachievableEditField.Position = [339 135 55 22];
% Create AveragetoobtainfromnowLabel
app.AveragetoobtainfromnowLabel = uilabel(app.UIFigure);
app.AveragetoobtainfromnowLabel.HorizontalAlignment = 'right';
app.AveragetoobtainfromnowLabel.VerticalAlignment = 'bottom';
app.AveragetoobtainfromnowLabel.Position = [-38 85 152 50];
app.AveragetoobtainfromnowLabel.Text = {'Average to'; 'obtain from now'};
% Create AveragetoobtainfromnowEditField
app.AveragetoobtainfromnowEditField = uieditfield(app.UIFigure, 'numeric');
app.AveragetoobtainfromnowEditField.ValueChangedFcn = createCallbackFcn(app, @AveragetoobtainfromnowEditFieldValueChanged, true);
app.AveragetoobtainfromnowEditField.Editable = 'off';
app.AveragetoobtainfromnowEditField.Position = [139 85 55 22];
% Create AddSubjectButton
app.AddSubjectButton = uibutton(app.UIFigure, 'push');
app.AddSubjectButton.ButtonPushedFcn = createCallbackFcn(app, @AddSubjectButtonPushed, true);
app.AddSubjectButton.Position = [19 194 120 22];
app.AddSubjectButton.Text = 'Add Subject';
% Create SaveTableScoresButton
app.SaveTableScoresButton = uibutton(app.UIFigure, 'push');
app.SaveTableScoresButton.ButtonPushedFcn = createCallbackFcn(app, @SaveTableScoresButtonPushed, true);
app.SaveTableScoresButton.Position = [293 194 120 22];
app.SaveTableScoresButton.Text = 'Save Table Scores';
% Create ThesismaxscoreEditFieldLabel
app.ThesismaxscoreEditFieldLabel = uilabel(app.UIFigure);
app.ThesismaxscoreEditFieldLabel.HorizontalAlignment = 'right';
app.ThesismaxscoreEditFieldLabel.Position = [228 35 99 22];
app.ThesismaxscoreEditFieldLabel.Text = 'Thesis max score';
% Create ThesismaxscoreEditField
app.ThesismaxscoreEditField = uieditfield(app.UIFigure, 'numeric');
app.ThesismaxscoreEditField.Limits = [0 100];
app.ThesismaxscoreEditField.ValueChangedFcn = createCallbackFcn(app, @ThesismaxscoreEditFieldValueChanged, true);
app.ThesismaxscoreEditField.Position = [338 35 55 22];
app.ThesismaxscoreEditField.Value = 7;
% Create TotalCFUEditFieldLabel
app.TotalCFUEditFieldLabel = uilabel(app.UIFigure);
app.TotalCFUEditFieldLabel.HorizontalAlignment = 'right';
app.TotalCFUEditFieldLabel.Position = [68 35 59 22];
app.TotalCFUEditFieldLabel.Text = 'Total CFU';
% Create TotalCFUEditField
app.TotalCFUEditField = uieditfield(app.UIFigure, 'numeric');
app.TotalCFUEditField.Limits = [0 200];
app.TotalCFUEditField.ValueChangedFcn = createCallbackFcn(app, @TotalCFUEditFieldValueChanged, true);
app.TotalCFUEditField.Position = [139 35 55 22];
app.TotalCFUEditField.Value = 120;
% Create AddSemesterButton
app.AddSemesterButton = uibutton(app.UIFigure, 'push');
app.AddSemesterButton.ButtonPushedFcn = createCallbackFcn(app, @AddSemesterButtonPushed, true);
app.AddSemesterButton.Position = [167 194 100 22];
app.AddSemesterButton.Text = 'Add Semester';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app_score_v4
% Create UIFigure and 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

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 5 Mar 2022

Commented:

on 6 Mar 2022

Community Treasure Hunt

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

Start Hunting!