How to input numeric array into Edit Filed using App Designer
77 views (last 30 days)
Show older comments
Hello
I have an easy tasks but I am begginer in App Designer, so I need your help
I have a vector like:
scale=[16,32,64,128,256];
I want to input it into Edit Field (maybe ListBox) and use it as needed, summarize for example. How can I do It
Thank you
0 Comments
Accepted Answer
Irma
on 10 May 2019
4 Comments
Adam Danz
on 10 May 2019
Edited: Adam Danz
on 10 May 2019
I see. You never mentioned that there were non-numeric characters in your strings - that would have been an easy problem to fix.
Anyway, there's 1 inefficiency in your function above.
If you're trying to convert a into a numeric vector, you don't need the while loop at all.
a=app.EditField_2.Value;
ac=[]
b=erase(a,{'[',']'})
a=split(b,{' ',','})
ac = str2double(a)'; %now you have a numeric vector
I've updated my answer to address the presence of non-numeric characters which can be cleaned up on 1-2 lines of code. This helped to make my anser more complete for others who might find it helpful so, thanks!
More Answers (2)
Adam Danz
on 8 May 2019
Edited: Adam Danz
on 16 Sep 2022
To assign vector of numbers to an Edit Field
There are two types of edit fields in app designer: numeric and text. Numeric edit fields may only receive double scalar values and cannot receive a vector. Text edit fields can receveive a character vector, a 1D cell array of char vectors, a string array, or a categorical array. Here's how to convert your vector to a string and enter it in a text field.
app.EditField.Value = num2str(scale);
To retrieve a vector of numbers from an Edit Field:
The value is stored as a cell array of strings. You must split apart the numbers stored as strings and then convert them to numeric form.
vec = str2double(strsplit(app.EditField.Value{1}));
% This assumes the vector is stored in the first element of the cell array
% NaN values indicate non-numeric segements
If the string contains non-numeric character such as '[7,8,9,10,11]', here's how to deal with that:
% UPDATE: This version only works with non-zero integers
str = app.ProfileparametersTextArea.Value{1};
str(~isstrprop(str,'digit')) = ' '; %replace non-numeric characters with empty space
vec = str2double(strsplit(strtrim(str))); % trim white space and convert to numeric
Updated version:
% This version works with negatives, decimals, etc.
str = '[7,-8,9,10,11]'
vec = strjoin(regexp(str,'(-)?\d+(\.\d+)?(e(-|+)\d+)?','match'),' ');
% or, if you want to keep the values separated into a cell array,
% a = regexp(str,'(-)?\d+(\.\d+)?(e(-|+)\d+)?','match')
To assign vector of numbers to a list box:
Items listed in a listbox for app designer are stored in the "Items" property. The value must be a 1D cell array of character vectors or a string array.
app.ListBox.Items = strsplit(num2str(scale));
To retrieve a vector of numbers from a list box:
Just convert the cell array of strings into numeric form.
vec = str2double(app.ListBox.Items) %the entire list vector
selection = str2double(app.ListBox.Value) %the number selected
16 Comments
Aditya
on 31 May 2023
This won't work if the numbers in string are inf, -inf
str(str == '[') = ' ';
str(str == ']') = ' ';
str(str == ',') = ' ';
str(str == ';') = ' ';
vec = str2double(strsplit(strtrim(str)));
The above seems to be a generically working solution.
To check for invalid input, see if vec contains NaNs. If that is the case, there is some invalid input.
Adam Danz
on 31 May 2023
Aditya
on 31 May 2023
Edited: Aditya
on 31 May 2023
The other solutions have a few basic edge cases where they fail:
- Do not consider inf, -inf
- When splitting, do not consider the fact that giving multitple slpit options will lead to more than expected values. For example, for the string 'str = [-10.9 , inf]', 'str = split(str, {' ', ',', ';'});' will create a 4 element character array.
One way to create a generic function is following:
function vec = string2Vector(str)
str = erase(str, {'[', ']'});
str = split(str, {' ', ',', ';'});
vec = str2double(str);
vec = vec(~isnan(vec));
end
1 Comment
Adam Danz
on 31 May 2023
The better option from my answer would be to use this line below, but it doesn't catch infs as you mentioned.
str = '[-10.9 , inf]'
vec = strjoin(regexp(str,'(-)?\d+(\.\d+)?(e(-|+)\d+)?','match'),' ')
There are issues with using the solution from your answer. For example,
str = '[0 1/3, -inf -4 inf 9e4 nan, 12.33]';
string2Vector(str)'
function vec = string2Vector(str)
str = erase(str, {'[', ']'});
str = split(str, {' ', ',', ';'});
vec = str2double(str);
vec = vec(~isnan(vec));
end
A more general solution might be to parse the string and use str2double within a loop but parsing wouldn't be straightforward.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!