MATLAB Answers

How to make an Edit Text Field box in App Designer to recognize a vector input

14 views (last 30 days)
I am using an Edit Field Text box for a user to input a numerical vector array. For example, I want the inputs to follow regular vector array notation 4:5 or 1:5:10 or just one number. I have found another users post where this question was partly answered. I was able to get it to recognize the inputs as a vector. But for an input such as 1:4 it would only see the numbers 1 and 4 [ 1 4 ] when it should have a vector so that it counts 1 through 4 [ 1 2 3 4 ].

Accepted Answer

Mohammad Sami
Mohammad Sami on 4 Jun 2020
The text field are designed to take in string inputs. It will return you whatever data is entered as string.
If you want to interpret this string, you have to write a function that parses the string in the manner you want it to be parsed. It is not recommended to use eval function with user inputs, as the user can then key in anything causing your program to break.
You have to decide what are the acceptable patterns of input and then write a parser that extracts these patterns for you.
In this case one of the input pattern you have is %i:%i. You can use regexp to extract these and other patterns.
stringvalue = '12:24';
out = regexp(stringvalue,'(?<d1>\d+):(?<d2>\d+)','names');
arr = str2double(out.d1):str2double(out.d2);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!