创建输入参数框

时间:2017-06-21 12:51:30

标签: matlab

我在MATLAB中创建一个显示框并输入由matlab代码

读取的输入值
prompt={'size1'};
name = 'Input';
defaultans = {'30'};
options.Interpreter = 'size1';
answer = inputdlg(prompt,name,[1 40],defaultans,options);

但是,我想以矩阵的形式输入size1的值 size1 = [2,4,7,10]

输入框中输入的所有值应在预先编写的代码的下一部分中标识,如下所示

t(:,1) = size1;

如果有人能告诉我如何相应地修改我的代码,将会很有帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用str2num函数将sting(输入)评估为矩阵。所以使用

获取矩阵
defaultans = {'[2,4,7,10]'};
answer = inputdlg('size1:','Input',1,defaultans);
% Click okay, answer='[2,4,7,10]'

matrixFromAnswer = str2num(answer{1});
% matrixFromAnswer = [2,4,7,8];

% Could check here if it is the right size etc. 
% For instance str2num will return [] if the input was invalid.

t(:,1) = size1(:); 
% The (:) ensures it is a column vector as you are assigning it to a column of t... 

<小时/>

或者,做这样的事情

answer = inputdlg('Enter your vector with a new line for each element:','Input',5);

% Input: 
%          3
%          2
%          1

matrixFromAnswer = str2num(answer{1});
% matrixFromAnswer = [3;2;1]

<小时/>

这实际上显示在Matlab的inputdlg documentation

示例2