How to input numeric array into Edit Filed using App Designer

167 views (last 30 days)
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

Accepted Answer

Irma
Irma on 10 May 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
  4 Comments

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 8 May 2019
Edited: Adam Danz on 10 May 2019
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:
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
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
  12 Comments
Adam Danz
Adam Danz on 29 Aug 2021
No need to install any toolboxes for str2num or str2double. What inputs are you providing?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!