How to display all the while loop at static text? (matlab GUI)
3 次查看(过去 30 天)
显示 更早的评论
How to display the while loop output at static text on GUI?
I want this output below
i=5
i=4
i=3
i=2
i=1
i=0
code
i = 5;
while i > 0
txt=fprintf('i = %d\n', i);
i = i - 1;
set(handles.ans, 'String',txt);
end
This code not working
0 个评论
采纳的回答
Jos
2015-4-4
Hi Abraham,
try this (assuming you added a push button to start the program and 'ans' is your static text box). Since txt will be a growing variable the size of each line has to be the same length, so if you want to start at i=10 change num2str(i,'%d') to num2str(i,'%02d') although this will result in output
i=10
i=09
i=08
etc
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
i = 5;
txt = [];
while i > 0
txt=[txt;'i = ' num2str(i,'%d')];
i = i - 1;
set(handles.ans, 'String',txt);
end
0 个评论
更多回答(1 个)
Geoff Hayes
2015-4-4
Abraham - if you step through this code (which is presumably in some sort of callback for one of your GUI controls) you would observe that the output from fprintf, txt, is a double and not a string (the carriage return/line break in the string is also problematic for single line text fields). Instead of the above, try the following with sprintf
for k=5:-1:1
txt=sprintf('k = %d', k);
set(handles.edit1, 'String',txt);
pause(1.0);
end
Note the use of the for loop and how we can decrement the counter. The pause for one second allows us to actually see the change to the edit control at each iteration of the loop.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Create Large-Scale Model Components 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!