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
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' }
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!