convert string in app designer to dropbox output

조회 수: 4 (최근 30일)
Jai Bassi
Jai Bassi 2019년 6월 4일
편집: Adam Danz 2019년 6월 4일
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
Guillaume
Guillaume 2019년 6월 4일
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 2019년 6월 4일
편집: Adam Danz 2019년 6월 4일
^^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);

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2019년 6월 4일
편집: Adam Danz 2019년 6월 4일
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
Guillaume
Guillaume 2019년 6월 4일
편집: Guillaume 2019년 6월 4일
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 2019년 6월 4일
편집: Adam Danz 2019년 6월 4일
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).

댓글을 달려면 로그인하십시오.

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by