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

7 ビュー (過去 30 日間)
James Thiem
James Thiem 2016 年 12 月 1 日
コメント済み: James Thiem 2016 年 12 月 1 日
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!!

採用された回答

Jan
Jan 2016 年 12 月 1 日
編集済み: Jan 2016 年 12 月 1 日
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 件のコメント
Jan
Jan 2016 年 12 月 1 日
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 2016 年 12 月 1 日
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

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2016 年 12 月 1 日
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
  1 件のコメント
James Thiem
James Thiem 2016 年 12 月 1 日
Hi. Thanks for the answer. Glad to solve it :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by