本文介绍了在MATLAB指南中,使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用for循环获取多个值.

I want to take multiple values with for loop.

firstVal   matlab.ui.control.NumericEditField

...

attributesNum = size(matrix,2) - 1 ;
X = zeros(attributesNum,1);
    for i=1:attributesNum
    values = app.firstVal;
    X(i,1) = values;
end

但是它不起作用.我该如何处理?

but it does not work. How can I handle this?

推荐答案

您可以通过添加等待,但是作为用户界面并没有多大意义……

You can do it by adding waitfor, but it doesn't make a lot of sense as a user interface...

以下是使用for循环获取多个值的示例:

Here is an example for taking multiple values with for loop:

for i = 1:attributesNum
    waitfor(app.firstVal, 'Value');

    value = app.firstVal.Value;
    X(i) = value;
end


此处是使用 MATLAB应用设计器构建的完整示例.
您可以将其复制并粘贴到m文件中以查看其工作方式.
大多数代码是由App Designer生成的.
相关代码在function startupFcn(app)中:


Here is a complete sample built using MATLAB App Designer.
You can copy and paste it to an m file to see how it works.
Most of the code was generated by App Designer.
The relevant code is in function startupFcn(app):

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
        EnteravalueandpressEnter5timesLabel  matlab.ui.control.Label
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            matrix = zeros(1, 5); % 1x5 elements matrix (just for example).

            attributesNum = size(matrix, 2);
            X = zeros(attributesNum, 1);

            for i = 1:attributesNum
                waitfor(app.firstVal, 'Value');

                value = app.firstVal.Value;
                X(i) = value;
                app.firstVal.Value = Inf; %Set to Inf every iteration, because waitfor waits for a change in value (and you may need to enter same value twice).

                %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
    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 EnteravalueandpressEnter5timesLabel
            app.EnteravalueandpressEnter5timesLabel = uilabel(app.UIFigure);
            app.EnteravalueandpressEnter5timesLabel.Position = [51 181 215 22];
            app.EnteravalueandpressEnter5timesLabel.Text = 'Enter a value and press Enter (5 times)';

            % 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)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            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


示例用户界面如下所示:


Here is how the sample user interface looks like:

这篇关于在MATLAB指南中,使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 19:44