print multiple lines to textarea

조회 수: 7 (최근 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개)

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
"

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by