Retrieve App Designer textarea content along with the newline characters

15 次查看(过去 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 中查找有关 Logical 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by