Hello everyone! I build a GUI for a main code that I have and based on the GUI, the user is supposed to enter a number, N. Once the number is entered, N is used to generate the list of a popup menu. example of list:
N = 3
User 1
User 2
User 3
my code and GUI functions properly, except when N is greater than 9. Then, instead of displaying User 10 in the pop up menu list, it is displaying User 1 User 0. This is occurring for all 2 digit numbers.
I tried a lot of options but none are actually working and I really need help! If anyone knows how to fix this, please help me!
This is the code I used:
for k = 2:(N+1)
string2{k} = sprintf('User %1.0g\n',(num2str((k-1),2)-48));
end
set(handles.popupmenu2,'String',string2)
Thank you

 채택된 답변

Geoff Hayes
Geoff Hayes 2017년 4월 8일

0 개 추천

Hosheeta - part of the problem might be your conversion from a number to a string via num2str and the subtraction of 48 (ASCII conversion?). Since you are using sprintf, you do not have to do this. Consider the following code instead
N=11;
for k = 1:N
string2{k} = sprintf('User %d',k);
end
Try the above and see what happens!

댓글 수: 1

Hosheeta Ramsurrun
Hosheeta Ramsurrun 2017년 4월 10일
Yes it worked! Thank you so much.... the num2str function was making the mistake.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by