フィルターのクリア

print multiple lines to textarea

6 ビュー (過去 30 日間)
Fransi Mittal
Fransi Mittal 2022 年 2 月 15 日
コメント済み: Fransi Mittal 2022 年 2 月 15 日
app.TextArea.Value=sprintf('Fourier Number of Terms:%d\n\nEquation:\ny=a0',n);
for i=1:n
txt='+a%d cos(%dx)+b%d sin(%dx)';app.TextArea.Value=(sprintf(txt,i,i,i,i));
end
app.TextArea.Value=sprintf('\nCoefficients:\na0=%.4f',a0);
for i=1:n; app.TextArea.Value=sprintf('\na%d=%.4f \nb%d=%.4f',i,a(i),i,b(i)) ;
end
app.TextArea.Value=sprintf('\n\nJ=%.4f \n\nR^2=%.4f \n\nRMSE=%.4f',j1,rsq1,RMSE1);
if i run this code it only prints last sprintf output, how do i print all
like this one

採用された回答

DGM
DGM 2022 年 2 月 15 日
編集済み: DGM 2022 年 2 月 15 日
You're replacing the contents of the textarea every time. If you want multiple lines, you'll need to concatenate them together.
EDIT:
Something like this. I can't run this on the site, but you can test it.
% placeholders
n = 1;
a0 = 1;
a = rand(1,n);
b = rand(1,n);
j1 = 1;
rsq1 = 1;
RMSE1 = 1;
% dummy figure
fig = uifigure;
app.TextArea = uitextarea(fig);
app.TextArea.Position(3:4) = [200 200];
alltext = sprintf('Fourier Number of Terms:%d\n\nEquation:\ny=a0',n);
for i=1:n
txt = '+a%d cos(%dx)+b%d sin(%dx)';
alltext = [alltext sprintf(txt,i,i,i,i)];
end
alltext = [alltext sprintf('\nCoefficients:\na0=%.4f',a0)];
for i=1:n
alltext = [alltext sprintf('\na%d=%.4f \nb%d=%.4f',i,a(i),i,b(i))];
end
alltext = [alltext sprintf('\n\nJ=%.4f \n\nR^2=%.4f \n\nRMSE=%.4f',j1,rsq1,RMSE1)];
app.TextArea.Value = alltext; % write once
  1 件のコメント
Fransi Mittal
Fransi Mittal 2022 年 2 月 15 日
Thank you, this works well

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

その他の回答 (1 件)

Benjamin Thompson
Benjamin Thompson 2022 年 2 月 15 日
Can you combine the outputs of all your sprintf calls into a single string, and pass this to app.TextArea.Value? It is not clear what app.TextArea.Value from your code, but my guess is that every time you assign something new to app.TextArea.Value, the previous information is deleted.
>> S1 = sprintf("My first string has number %d\n", 3)
S1 =
"My first string has number 3
"
>> S2 = sprintf("My second string has name %s\n", "Billy")
S2 =
"My second string has name Billy
"
>> S3 = strcat(S1, S2)
S3 =
"My first string has number 3
My second string has name Billy
"
  1 件のコメント
Fransi Mittal
Fransi Mittal 2022 年 2 月 15 日
thanks, I got it

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by