I want to collect data to cell array in Matlab App Designer.

39 views (last 30 days)
Hi,
l am trying to make an app that takes some input from user and makes calculations, and then collects the data (in cell array or something).
After that, input boxes should set to zero to allow user to input other cases. My goal is collecting all the data from user, and allow user to get the result for a sepecific case if wanted.
I couldn't make the collection part for more than 1 case.
Thank you in advance.
  2 Comments
piston_pim_offset
piston_pim_offset on 28 Oct 2023
If l get how to collect data to in order, l can finish my app (hopely).
piston_pim_offset
piston_pim_offset on 2 Nov 2023
Edited: piston_pim_offset on 2 Nov 2023
Isn not there anyone who can help me about this problem?
I tried to use a loop inside the button function and give a value to loop variable in a matlab script, but function of the button did not recognize "i=1; "
i=1;
run xxx.mlapp

Sign in to comment.

Accepted Answer

Voss
Voss on 2 Nov 2023
Here is an app that collects data entered into two numeric editfields into matrix, and also performs some calculations and stores the results in a cell array.
To use:
  1. Enter some numbers.
  2. Hit the Save button to store the numbers, perform some calculations, store the results, and reset the editfields to 0.
  3. Hit the Done button to save all the data and results collected so far into a mat file and close the application.
Hopefully you can get some ideas about what to do with your real app from this simple app.
  2 Comments
piston_pim_offset
piston_pim_offset on 6 Nov 2023
Thank you for your suggestion. I somehow figured it out adding "private property" that has 'i=1'. With this, i is defined in all functions, which enabled me to store the data in a cell array by writing 'i=i+1' a the end of the button function.
l am using Matlab Online, so it would be great if you can share the code in comments.
Best regards
Voss
Voss on 6 Nov 2023
classdef test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SaveButton matlab.ui.control.Button
Data2EditField matlab.ui.control.NumericEditField
Data2EditFieldLabel matlab.ui.control.Label
Data1EditField matlab.ui.control.NumericEditField
Data1EditFieldLabel matlab.ui.control.Label
DoneButton matlab.ui.control.Button
end
properties (Access = private)
data = [];
results = {};
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: SaveButton
function cb_save(app, event)
% store the EditFields' current values in a new row of app.data:
data1 = app.Data1EditField.Value;
data2 = app.Data2EditField.Value;
app.data(end+1,:) = [data1 data2];
% perform some calculations:
result = {data1*data2, data1+data2, data1/data2};
% store the result in a new element of app.results:
app.results{end+1} = result;
% reset the EditFields' values to 0:
app.Data1EditField.Value = 0;
app.Data2EditField.Value = 0;
end
% Button pushed function: DoneButton
function cb_done(app, event)
% save app.data and app.results to a mat file:
data = app.data; %#ok<ADPROPLC>
results = app.results; %#ok<ADPROPLC>
save('data_and_results.mat','data','results');
% close the application:
close(app.UIFigure);
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 214 132];
app.UIFigure.Name = 'MATLAB App';
% Create DoneButton
app.DoneButton = uibutton(app.UIFigure, 'push');
app.DoneButton.ButtonPushedFcn = createCallbackFcn(app, @cb_done, true);
app.DoneButton.Position = [121 15 70 22];
app.DoneButton.Text = 'Done';
% Create Data1EditFieldLabel
app.Data1EditFieldLabel = uilabel(app.UIFigure);
app.Data1EditFieldLabel.HorizontalAlignment = 'right';
app.Data1EditFieldLabel.Position = [32 94 44 22];
app.Data1EditFieldLabel.Text = 'Data 1:';
% Create Data1EditField
app.Data1EditField = uieditfield(app.UIFigure, 'numeric');
app.Data1EditField.Position = [91 94 100 22];
% Create Data2EditFieldLabel
app.Data2EditFieldLabel = uilabel(app.UIFigure);
app.Data2EditFieldLabel.HorizontalAlignment = 'right';
app.Data2EditFieldLabel.Position = [32 57 44 22];
app.Data2EditFieldLabel.Text = 'Data 2:';
% Create Data2EditField
app.Data2EditField = uieditfield(app.UIFigure, 'numeric');
app.Data2EditField.Position = [91 57 100 22];
% Create SaveButton
app.SaveButton = uibutton(app.UIFigure, 'push');
app.SaveButton.ButtonPushedFcn = createCallbackFcn(app, @cb_save, true);
app.SaveButton.Position = [31 15 70 22];
app.SaveButton.Text = 'Save';
% 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 = test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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.

More Answers (0)

Categories

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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!