How to input numeric array into Edit Filed using App Designer
84 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Irma
2019-5-10
4 个评论
Adam Danz
2019-5-10
编辑:Adam Danz
2019-5-10
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!
更多回答(2 个)
Adam Danz
2019-5-8
编辑:Adam Danz
2022-9-16
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 个评论
Aditya
2023-5-31
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
2023-5-31
Aditya
2023-5-31
编辑:Aditya
2023-5-31
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 个评论
Adam Danz
2023-5-31
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!