Borrar filtros
Borrar filtros

Retrieve App Designer textarea content along with the newline characters

15 visualizaciones (últimos 30 días)
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 comentario
Adam Danz
Adam Danz el 22 de Feb. de 2023
> 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'

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 22 de Feb. de 2023
Editada: Voss el 22 de Feb. de 2023
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 comentario
Prabhakar Vallury
Prabhakar Vallury el 22 de Feb. de 2023
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by