I want to take multiple values with waitfor
operator but my code does not work. Where is my mistake?
methods (Access = private)
% Callback function
function ButtonPushed(app, event)
%Reading dataset and getting the inputs from user by using input operation
matrix = xlsread("Transfusion.xlsx");
attributesNum = size(matrix,2) - 1 ;
X = zeros(attributesNum,1);
for i=1:attributesNum
waitfor(app.firstVal, 'Value');
value = app.firstVal.Value;
X(i,1) = value;
%Update text of ValuesLabel (for demostrating the concept).
text = ['Values: ', sprintf('%.1f, ', X(1:i))];
app.ValuesLabel.Text = text(1:end-2);
end
%Display X in Command window for testing
disp(X)
...
end
I think it should have worked...
appdesigner
in command window). firstVal
in MATLAB App Designer: Inf
is an improvised solution, but it's important, because waitfor
waits for the value to get changed.Inf
after entering a new value forces the user to change the value. matrix = xlsread("Transfusion.xlsx");
with matrix = [0, 0, 0, 0, 0, 0];
."Transfusion.xlsx"
.Here is a modified version of the function ButtonPushed(app, event)
:
% Button pushed function: Button
function ButtonPushed(app, event)
app.Button.Enable = 'Off'; %Disable button while taking input (just nicer).
%I replaced the xlsread("Transfusion.xlsx") with some arbitrary values, because only the size is relevant
matrix = [0, 0, 0, 0, 0, 0]; %xlsread("Transfusion.xlsx");
attributesNum = size(matrix,2) - 1;
X = zeros(attributesNum, 1);
%Initialize text label with message
app.ValuesLabel.Text = ['Enter ', num2str(attributesNum), ' values in edit box (set value and press enter)'];
for i = 1:attributesNum
waitfor(app.firstVal, 'Value');
value = app.firstVal.Value;
X(i, 1) = value;
%Set to Inf every iteration, because waitfor waits for a change in value (and you may need to enter same value twice).
app.firstVal.Value = Inf;
%Update text of ValuesLabel (for demonstrating the concept).
text = ['Values: ', sprintf('%.1f, ', X(1:i))];
app.ValuesLabel.Text = text(1:end-2);
end
%Display X in Command window for testing
disp(X)
app.Button.Enable = 'On'; %Enable button at the end.
end
Here is the complete code (including generated code).
You can copy and paste it to App1.m
file just to see how it works.
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
EditFieldLabel matlab.ui.control.Label
firstVal matlab.ui.control.NumericEditField
ValuesLabel matlab.ui.control.Label
Button matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
app.Button.Enable = 'Off'; %Disable button while taking input (just nicer).
%I replaced the xlsread("Transfusion.xlsx") with some arbitrary values, because only the size is relevant
matrix = [0, 0, 0, 0, 0, 0]; %xlsread("Transfusion.xlsx");
attributesNum = size(matrix,2) - 1;
X = zeros(attributesNum, 1);
%Initialize text label with message
app.ValuesLabel.Text = ['Enter ', num2str(attributesNum), ' values in edit box (set value and press enter)'];
for i = 1:attributesNum
waitfor(app.firstVal, 'Value');
value = app.firstVal.Value;
X(i, 1) = value;
%Set to Inf every iteration, because waitfor waits for a change in value (and you may need to enter same value twice).
app.firstVal.Value = Inf;
%Update text of ValuesLabel (for demonstrating the concept).
text = ['Values: ', sprintf('%.1f, ', X(1:i))];
app.ValuesLabel.Text = text(1:end-2);
end
%Display X in Command window for testing
disp(X)
app.Button.Enable = 'On'; %Enable button at the end.
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 369 256];
app.UIFigure.Name = 'UI Figure';
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.Position = [45 123 56 22];
app.EditFieldLabel.Text = 'Edit Field';
% Create firstVal
app.firstVal = uieditfield(app.UIFigure, 'numeric');
app.firstVal.Position = [116 123 100 22];
app.firstVal.Value = Inf;
% Create ValuesLabel
app.ValuesLabel = uilabel(app.UIFigure);
app.ValuesLabel.Position = [49 51 297 22];
app.ValuesLabel.Text = 'Values: ';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Tooltip = {'Press to start taking input'};
app.Button.Position = [46 204 120 32];
% 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 = app1
% 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