Retrieve App Designer textarea content along with the newline characters

19 ビュー (過去 30 日間)
Prabhakar Vallury
Prabhakar Vallury 2023 年 2 月 22 日
コメント済み: Prabhakar Vallury 2023 年 2 月 22 日
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 Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by