热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在MATLAB的一个框中从用户处获取多个输入

我想使用waitfor运算符获取多个值,但是我的代码不起作用。我的错误在哪里?

我想使用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

enter image description here


  • 将值设置为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

推荐阅读
  • 本文介绍如何通过注册表编辑器自定义和优化Windows文件右键菜单,包括删除不需要的菜单项、添加绿色版或非安装版软件以及将特定应用程序(如Sublime Text)添加到右键菜单中。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文探讨了Hive中内部表和外部表的区别及其在HDFS上的路径映射,详细解释了两者的创建、加载及删除操作,并提供了查看表详细信息的方法。通过对比这两种表类型,帮助读者理解如何更好地管理和保护数据。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
author-avatar
燕门雪_346
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有