How to input numeric array into Edit Filed using App Designer
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (2)
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 commentaires
Irma
le 9 Mai 2019
Adam Danz
le 10 Mai 2019
vec = str2double(strsplit(app.ProfileparametersTextArea.Value{1}));
% This assumes the vector is stored in the first element of the cell array
% NaN values indicate non-numeric segements
I couldn't run this
if you've anderstand my question write me hool code, please
What does it mean "I couldn't run this?" Did you get an error (if so, what was the error message)? You need to replace my handles with your handles.
Please provide me with the value stored here:
app.EditField.Value % make sure you use YOUR handles (not mine)
% |_________| put your text box handle here
Irma
le 10 Mai 2019
I don't mind the answer selection - I'm not a point hunter ;)
You have to remember that people don't know what type of data you're working with. I can't imagine what your text box contains that breaks that code.
Often times you'll have to slightly adapt the proposed solutions to your needs. Some people expect a copy-paste-ready solution and often times that's possible but not always. In the end, the person seeking advice should always study the solution line by line to understand how it works and adapt it to their needs, if needed.
In this case, the error message you received suggests the solution to eliminate the error: You probably just needed to remove the {1}.
Patrizio Graziosi
le 27 Jan 2020
Modifié(e) : Patrizio Graziosi
le 27 Jan 2020
Hi all,
how can I input an array in the form e.g. 100:50:1000 ?
The numeric editor does not get the ' : ', using the text editor I cannot get the array...
I cannot work it out, can you help me, please?
Thanks
The values entered in a numeric edit field must be scalar (single values).
If you want to display a vector within an edit field, you'll have to use the one that accepts character vectors and string scalars.
% To display the colon notation
app.EditField.Value = '100:50:1000';
% To display the full vector, replacing excess white space
% with a single space between values.
app.EditField.Value = strjoin(compose('%d',100:50:1000));
% Or, prior to r2016b
app.EditField.Value = regexprep(strtrim(num2str(100:50:1000)),' +',' ');
Jon
le 14 Jan 2021
Thanks for your work on this. Regarding the portion:
If the string contains non-numeric character such as '[7,8,9,10,11]', here's how to deal with that:
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
I may be missing something, but I seem to have two problems with using this. First it seems that the .Value property is a character vector not a cell array so the {1} are not needed and in fact cause an error. More important, it seems that
str(~isstrprop(str,'digit')) = ' '
also clears out any negative signs, and decimal places so this code will only work for vectors of positive integers. Maybe you mention that somewhere, but if so I missed it.
Adam Danz
le 14 Jan 2021
Premkumar Eramuthu
le 29 Août 2021
I don't know why str2num or str2double doesn't work in my code in the app designer part. Can you please tell me how to fix this? Actually do I need to install any MATLAB or simulink toolbox though str2num is an inbuilt function?
Adam Danz
le 29 Août 2021
No need to install any toolboxes for str2num or str2double. What inputs are you providing?
Samuel David Solano Celis
le 4 Sep 2022
To retrieve a vector of numbers from an Edit Field
I also have negative numbers, but when i convert it to vector it convert them all in positive. how can i fix it?
Adam Danz
le 16 Sep 2022
@Samuel David Solano Celis, great point, thanks for pointing that out. I've updated that section of the answer.
Aditya
le 31 Mai 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.
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 commentaire
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.
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!