convert string in app designer to dropbox output

4 visualizaciones (últimos 30 días)
Jai Bassi
Jai Bassi el 4 de Jun. de 2019
Editada: Adam Danz el 4 de Jun. de 2019
function NoNodesEditFieldValueChanged(app, event)
app.nN = str2double(app.NoNodesEditField.Value);
N_str = num2str((1:app.nN)');
app.NodeNoDropDown.Items = {(N_str)} ;
app.NodeNoDropDown_2.Items = num2cell(N_str);
app.NodeNoDropDown_3.Items = cellstr(N_str);
end
The idea is to take a value someone enters (say 7) and create options for the outputs of drop downs that range from 1-7. Currently all methods either end up not working or display NaN
  4 comentarios
Guillaume
Guillaume el 4 de Jun. de 2019
See my comment to Adam's answer for a much better way of generating that list of items (and other recommendations). Do not continue using the code you show which is inefficient.
Adam Danz
Adam Danz el 4 de Jun. de 2019
Editada: Adam Danz el 4 de Jun. de 2019
^^Agreed. Guillaume's compose() suggestion is much more efficient.
I compared the median speeds of 100,000 repetitions of the following lines:
compose('%d', 1:10);
% vs
strsplit(num2str(1:10)); % as suggested in my answer
and compose() was 4x faster (p < 0.0001; wilcoxon signed rank test);
compose('%d', 1:10);
% vs
cellstr(num2str((1:10)')); % as in your code above
and compose() was 5.3x faster (p < 0.0003; wilcoxon signed rank test);

Iniciar sesión para comentar.

Respuestas (1)

Adam Danz
Adam Danz el 4 de Jun. de 2019
Editada: Adam Danz el 4 de Jun. de 2019
This line is causing the problem
app.nN = str2double(app.NoNodesEditField.Value);
because app.NoNodesEditField.Value is already a 'double' and you're treating it as if it were a string which produces a NaN.
This should work:
function NoNodesEditFieldValueChanged(app, event)
app.nN = app.NoNodesEditField.Value;
app.NodeNoDropDown.Items = strsplit(num2str(1:app.nN))
end
  2 comentarios
Guillaume
Guillaume el 4 de Jun. de 2019
Editada: Guillaume el 4 de Jun. de 2019
app.NoNodesEditField.Value is already a 'double'
It's not been specified if the type of the uieditfield is numeric or text, so Value could be either text or numeric. In this case, it would indeed make sense to use a numeric edit field.
A much better way to generate the Item list would be:
app.NodeNoDropDown.Items = compose('%d', 1:app.nN); %directly generate the required cell array
I would recommend populating the ItemData property at the same time:
app.NodeNoDropDown.ItemData = 1:app.nN;
This way the Value property returned by the dropdown will be numeric instead of char.
Adam Danz
Adam Danz el 4 de Jun. de 2019
Editada: Adam Danz el 4 de Jun. de 2019
Thanks for the compse() recommendation, that's one I haven't used (yet).
If the uieditfield is numeric and a numeric value is put into str2double(), it would produce a NaN which would propagate through in all of the examples provided by OP. If the uieditfield is text and the string is a number, then the last two options the OP listed should work but the OP says they don't work. That's why I deducted that the uieditfield is numeric (unless it is string and the string is not a number).

Iniciar sesión para comentar.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by