How to input numeric array into Edit Filed using App Designer

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

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

4 commentaires

Adam Danz
Adam Danz le 10 Mai 2019
Modifié(e) : Adam Danz le 8 Déc 2020
Unless you're proposing an answer, please use the comment sections.
Irma
Irma le 10 Mai 2019
Modifié(e) : Irma le 10 Mai 2019
I have solved this problem myself programically
function ButtonPushed(app, event)
a=app.EditField_2.Value;
ac=[]
b=erase(a,{'[',']'})
a=split(b,{' ',','})
i=1;
while i<=length(a)
ac=[ac,str2double(a(i))]
i=i+1;
end
app.Label.Text=num2str(sum(ac))
end
if my input is : [1 2 3 4] answer in Label will be 10, so it works!
I just wanted to do easyer than here
Adam Danz
Adam Danz le 10 Mai 2019
Modifié(e) : Adam Danz le 10 Mai 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!
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (2)

Adam Danz
Adam Danz le 8 Mai 2019
Modifié(e) : Adam Danz le 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 commentaires

app.EditField = num2str(scale);
this code sets value to an app.EditField component from vector scale , I want to do vice versa
set scale from app.EditField
I have tried like this, but it doesn't work
Adam Danz
Adam Danz le 9 Mai 2019
Modifié(e) : Adam Danz le 10 Mai 2019
I've updated my answer to show how to retrieve the values in a text field or list box as numeric values. Does that hit your target?
Irma Davitashvili comment moved here:
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
Adam Danz
Adam Danz le 10 Mai 2019
Modifié(e) : Adam Danz le 10 Mai 2019
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
I tried run the code you write me:
function Button2Pushed(app, event)
str = app.EditField_2.Value{1};
str(~isstrprop(str,'digit')) = ' '; %replace non-numeric characters with empty space
vec = str2double(strsplit(strtrim(str)));
end
but still Error :
Cell contents reference from a non-cell array object.
on line : str = app.EditField_2.Value{1};
so I prefer to choose last one (below) in Accepted Answer
Thanks!
Adam Danz
Adam Danz le 10 Mai 2019
Modifié(e) : Adam Danz 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
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
Adam Danz
Adam Danz le 27 Jan 2020
Modifié(e) : Adam Danz le 27 Jan 2020
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)),' +',' ');
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.
Thanks Jon, the thread became messy when the OP continued the conversation under their own answer.
I pointed out the problem with the cell indexing in a comment above.
The isstrprop is a good suggestion. I suggested a less flexible solution as a comment under the OP's answer.
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?
No need to install any toolboxes for str2num or str2double. What inputs are you providing?
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?
@Samuel David Solano Celis, great point, thanks for pointing that out. I've updated that section of the answer.
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.
I would suggest using strrep or regexprep to replace the brackets, commas, etc, rather than this approach.
str = '[5, 4 6 12 inf -12];'
str = '[5, 4 6 12 inf -12];'
strClean = regexprep(str, {'[',']',',',';'}, '')
strClean = '5 4 6 12 inf -12'

Connectez-vous pour commenter.

Aditya
Aditya le 31 Mai 2023
Modifié(e) : Aditya le 31 Mai 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 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]'
str = '[-10.9 , inf]'
vec = strjoin(regexp(str,'(-)?\d+(\.\d+)?(e(-|+)\d+)?','match'),' ')
vec = '-10.9'
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)'
ans = 1×6
1.0e+04 * 0 -Inf -0.0004 Inf 9.0000 0.0012
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.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by