作者:燕门雪_346 | 来源:互联网 | 2023-09-23 16:50
我想使用waitfor
运算符获取多个值,但是我的代码不起作用。我的错误在哪里?
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
我认为它应该有效...
- 确保使用MATLAB App Designer(您可以通过在命令窗口中输入
appdesigner
来启动它)。
- 确保在MATLAB App Designer中将编辑框命名为
firstVal
:
- 将值设置为
Inf
是一种即兴解决方案,但这很重要,因为waitfor
等待值被更改。
输入新值后将值设置为Inf
会强制用户更改值。
- 我将
matrix = xlsread("Transfusion.xlsx");
替换为matrix = [0,0];
。
备注:不要期望我们猜测"Transfusion.xlsx"
的内容。
该文件与帖子无关,因为您仅使用大小。
- 备注:不要害羞,并张贴完整的App Designer代码作为参考(当使用类似App Designer的工具时,该工具可能会在自动生成的代码中隐藏一些重要信息,这对于识别问题可能很重要)。
- 备注:由于这是后续帖子,因此您应参考自己的previous post。
这是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]; %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
这是完整的代码(包括生成的代码)。
您可以将其复制并粘贴到App1.m
文件中以查看其工作方式。
classdef app1 % 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]; %xlsread("Transfusion.xlsx");
attributesNum = size(matrix,2) - 1;
X = zeros(attributesNum,1);
%Initialize text label with message
app.ValuesLabel.Text = ['Enter ',' 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: ',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