Retrieve App Designer textarea content along with the newline characters

조회 수: 19 (최근 30일)
Matlab version: 2019b
In Appdesigner, I'm populating a text-area named Status_TxtArea with a single string containing newline breaks as in the example shown below:
txtmsg = sprintf("Matlab version: %s\Something else: %s\nAnother thing: %s", version, variable-1, variable-2);
app.Status_TxtArea.Value = txtmsg
I would like to later retrieve this context from the textarea INCLUDING THE NEWLINE breaks for sending an email. For example:
email_body = app.Status_TxtArea.Value
The problem is that the contents are coming out 'squashed' into a single line without the linebreaks and it looks awful. Is there any easy way to accomplish this? I could put unique delimiters at the end of each line and split them up, but was hoping that wouldn't be necessary. TIA
  댓글 수: 1
Adam Danz
Adam Danz 2023년 2월 22일
> The problem is that the contents are coming out 'squashed' into a single line without the linebreaks and it looks awful.
That's not what I see.
First, your example is missing the first "n" in "Matlab version: %s\Something". If you add the n, I see the follwoing (R2022b)
txtmsg = sprintf("Matlab version: %s\nSomething else: %s\nAnother thing: %s", '1.0', '12', '23');
app.Status_TxtArea = uitextarea(uifigure());
app.Status_TxtArea.Value = txtmsg;
app.Status_TxtArea.Value =
ans =
3×1 cell array
{'Matlab version: 1.0'}
{'Something else: 12' }
{'Another thing: 23' }
which, as @Voss explains, is the expected results when using line breaks.
To combine that back into a character vector, you can use,
str = strjoin(app.Status_TxtArea.Value',newline)
str =
'Matlab version: 1.0
Something else: 12
Another thing: 23'

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 2월 22일
편집: Voss 2023년 2월 22일
The multi-line text is stored as a cell array of character vectors in the textarea's Value, so to present it with newlines, you can sprintf it with a newline after each element of the cell array:
email_body = sprintf('%s\n',app.Status_TxtArea.Value{:});
Note that this gives you an extra newline at the end compared to your ogininal textmsg. If that's a problem, you can remove it with strtrim:
email_body = strtrim(sprintf('%s\n',app.Status_TxtArea.Value{:}));
Or use strjoin instead of sprintf:
email_body = strjoin(app.Status_TxtArea.Value,newline());
Tested in R2017b and R2022a.
  댓글 수: 1
Prabhakar Vallury
Prabhakar Vallury 2023년 2월 22일
Ah, this is perfect. I wasn't aware of the strjoin function, was just using string(app.Status_TxtArea.Value) and that was the problem. Thanks for your brilliance!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by