Diana
Diana

Reputation: 102

Create input parameter box

I create a display box in MATLAB and enter input values which are read by matlab code

prompt={'size1'};
name = 'Input';
defaultans = {'30'};
options.Interpreter = 'size1';
answer = inputdlg(prompt,name,[1 40],defaultans,options);

However, I want to enter values of size1 in the form of matrix like size1 = [2,4,7,10]

All the values entered in input box,should be identified in the next part of prewritten code which is as follows

t(:,1) = size1;

It would be helpful if anyone can let me know how can i modify my code accordingly.

Upvotes: 0

Views: 43

Answers (1)

Wolfie
Wolfie

Reputation: 30047

You can make use of the str2num function to evaluate a sting (the input) as a matrix. So get the matrix using

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... 


Alternatively, do something like this

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]


This is actually shown in Example 2 of Matlab's inputdlg documentation

Upvotes: 1

Related Questions