How to display all the while loop at static text? (matlab GUI)
Show older comments
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
Accepted Answer
More Answers (1)
Geoff Hayes
on 4 Apr 2015
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!