【问题标题】:How to pass the numerical values inputted from a GUI to a .m file?如何将从 GUI 输入的数值传递给 .m 文件?
【发布时间】:2021-02-15 03:50:12
【问题描述】:

我正在尝试在我的 .m/ GUI 文件中编写一个代码,当我按下按钮时,它可以将从 GUI 中的“编辑文本”字段(称为“编辑 1”)获得的某个值传递给.m 文件。 在 GUI 中说我有这个: GUI example 我想要做的是获取在“fc”和“slump”字段中输入的值,并将其用于 .m 文件中的数学运算。在 GUI 中,我编写了代码以在“计算”按钮回调下方运行我的 m 文件,在我的 .m 文件中,我编写了:

value = str2num(get(handles.edit1,'String'));

作为回报,我在运行 UI 时会收到 Undefined variable "handles" or class "handles.edit1"。我应该在我的 .m 文件或 GUI 编辑器中写什么来获取这些值?

非常感谢任何解决方案/帮助。谢谢!

【问题讨论】:

    标签: matlab user-interface matlab-guide matlab-gui


    【解决方案1】:

    以编程方式创建 GUI 和回调函数

    不确定您对以编程方式执行此操作有多开放,但这里有一个设置,它获取字段的值并在名为 Calculate() 的回调函数中使用它们。另一个清除字段的回调函数称为Reset()。下面是显示在命令窗口中的 GUI 和回调函数的输入:

    .ButtonPushedFcn属性设置为需要运行的相应函数指示按下按钮时触发回调函数。

    完整脚本:

    %******************************************************%
    %GUI ELEMENT SETUP/PROPERTIES%
    %******************************************************%
    
    %App figure properties%
    App = uifigure();
    App.Name = "GUI";
    X_Position = 200;
    Y_Position = 200;
    Height = 300;
    Width = 600;
    App.Position = [X_Position Y_Position Width Height];
    
    %Requirements sub-panel properties% 
    Input_Panel = uipanel('Parent',App);
    Input_Panel.Title = "Requirements";
    Input_Panel.TitlePosition = 'centertop';
    Input_Panel.FontWeight = 'bold';
    X_Position = 50;
    Y_Position = 75;
    Height = 120;
    Width = 250;
    Input_Panel.Position = [X_Position Y_Position Width Height];
    
    %Button label 1 properties%
    Label_1 = uilabel('Parent',Input_Panel);
    X_Position = 15;
    Y_Position = 20;
    Height = 30;
    Width = 60;
    Label_1.Position = [X_Position Y_Position Width Height];
    Red = 0.2; Green = 0.2; Blue = 0.2;
    Label_1.BackgroundColor = [Red Green Blue];
    Label_1.FontColor = "white";
    Label_1.HorizontalAlignment = "center";
    Label_1.Text = "Slump'";
    
    %Button label 2 properties%
    Label_2 = uilabel('Parent',Input_Panel);
    X_Position = 15;
    Y_Position = 60;
    Height = 30;
    Width = 60;
    Label_2.Position = [X_Position Y_Position Width Height];
    Red = 0.2; Green = 0.2; Blue = 0.2;
    Label_2.BackgroundColor = [Red Green Blue];
    Label_2.FontColor = "white";
    Label_2.HorizontalAlignment = "center";
    Label_2.Text = "fc'";
    
    %FC field properties%
    FC_Field = uieditfield('Parent',Input_Panel);
    X_Position = 100;
    Y_Position = 60;
    Height = 30;
    Width = 90;
    FC_Field.Position = [X_Position Y_Position Width Height];
    
    %Slump field properties%
    Slump_Field = uieditfield('Parent',Input_Panel);
    X_Position = 100;
    Y_Position = 20;
    Height = 30;
    Width = 90;
    Slump_Field.Position = [X_Position Y_Position Width Height];
    
    %Mpa label properties%
    Mpa_Label = uilabel('Parent',Input_Panel);
    X_Position = 200;
    Y_Position = 60;
    Height = 30;
    Width = 80;
    Mpa_Label.Position = [X_Position Y_Position Width Height];
    Mpa_Label.Text = "MPa";
    
    %Cm label properties%
    Cm_Label = uilabel('Parent',Input_Panel);
    X_Position = 200;
    Y_Position = 20;
    Height = 30;
    Width = 80;
    Cm_Label.Position = [X_Position Y_Position Width Height];
    Cm_Label.Text = "cm";
    
    %Calculate button properties%
    Calculate_Button = uibutton('Parent',App);
    X_Position = 400;
    Y_Position = 150;
    Height = 40;
    Width = 160;
    Calculate_Button.Position = [X_Position Y_Position Width Height];
    Calculate_Button.Text = "Calculate";
    Red = 0.7; Green = 0.7; Blue = 0.7;
    Calculate_Button.BackgroundColor = [Red Green Blue];
    
    %Reset button properties%
    Reset_Button = uibutton('Parent',App);
    X_Position = 420;
    Y_Position = 105;
    Height = 35;
    Width = 120;
    Reset_Button.Position = [X_Position Y_Position Width Height];
    Reset_Button.Text = "Reset";
    Reset_Button.FontWeight = 'bold';
    Reset_Button.FontSize = 20;
    Red = 0.7; Green = 0.7; Blue = 0.7;
    Reset_Button.BackgroundColor = [Red Green Blue];
    
    %Callback function configurations%
    Calculate_Button.ButtonPushedFcn = @(Calculate_Button,event) Calculate(FC_Field,Slump_Field);
    Reset_Button.ButtonPushedFcn = @(Calculate_Button,event) Reset(FC_Field,Slump_Field);
    
    %******************************************************%
    %CALLBACK FUNCTION DEFINITIONS%
    %******************************************************%
    
    %Calculate callback function definition%
    function [] = Calculate(FC_Field,Slump_Field)
    
    fprintf("Calculating Using:\n");
    fprintf("fc': %f\n",str2num(FC_Field.Value));
    fprintf("Slump: %f\n",str2num(Slump_Field.Value));
    
    end
    
    %Reset callback function definition%
    function [] = Reset(FC_Field,Slump_Field)
    fprintf("Resetting/clearing fields\n");
    FC_Field.Value = "";
    Slump_Field.Value = "";
        
    end
    

    使用 MATLAB R2019b 运行

    【讨论】:

    • 感谢 MichaelTr7 的彻底和清晰的回答以及 GUI 重制!非常感激。帮了大忙。
    • @yacika 没问题,很乐意提供帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2013-12-02
    • 2014-04-30
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多