print multiple lines to textarea
6 views (last 30 days)
Show older comments
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/895505/image.png)
like this one
0 Comments
Accepted Answer
DGM
on 15 Feb 2022
Edited: DGM
on 15 Feb 2022
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
More Answers (1)
Benjamin Thompson
on 15 Feb 2022
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
"
See Also
Categories
Find more on Characters and Strings 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!