How to display sprintf in a 'for' loop in static text of GUI?

7 views (last 30 days)
Greetings. I wish to display a sprintf output as in command window. The output should look like this:
My goal is to display the same output in my GUI static text box, but my code couldn't work.
The function rk3 is runge-kutta 3rd order:
E1=rk3(f,df,a,b,ya,M);
for j=1:M+1
message1 = message1 + sprintf('%5d %10.5f %10.5f %10.5f\n' , E1(j,1) , E1(j,2) , E1(j,3) , E1(j,4));
end
set(handles.text8,'String',message1);
Your help is much appreciated. Thanks!!

Accepted Answer

Jan
Jan on 1 Dec 2016
Edited: Jan on 1 Dec 2016
Whenever you mention "did not work" in the forum, add the complete error message or explain the difference between the results and your expectations. Do not let the readers guess, what's going on.
You cannot add strings by "A+B" in Matlab. Use "[A, B]" instead.
The loop is not required:
message = sprintf('%5d %10.5f %10.5f %10.5f\n' , E1.'); % Or: E1(1:M+1, 1:4).'
  3 Comments
Jan
Jan on 1 Dec 2016
format short has effects in the command line only. Where do you tried "%1d"? Is your problem the "0" in the first place? There is not output format which removes the decimals for 0 only. If you need this, you have to prgram a small workaround by your own.
If you post the text as text and not as screen shot, working with the data is easier.
James Thiem
James Thiem on 1 Dec 2016
Hi, I tried %1d in here
message = sprintf('%1d %10.5f %10.5f %10.5f\n' , E1.');
but I solved it using
message = sprintf('%.2f %10.5f %10.5f %10.5f\n' , E1.');
Thanks :D

Sign in to comment.

More Answers (1)

KSSV
KSSV on 1 Dec 2016
How about this?
fprintf('%s %s %s %s \n','t','Ytrue','Yrk3' ,'|Et|%')
for i = 1:5
fprintf('%f %f %f %f \n',rand(1),rand(1),rand(1),rand(1))
end

Categories

Find more on Data Type Conversion 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!