append (not strcat) to combine strings with whitespace (\n)

조회 수: 13 (최근 30일)
Gavin
Gavin 2024년 8월 1일
답변: Walter Roberson 2024년 8월 6일
I have a text box (text edit field, shoud it be a label box?)
I want to add lines of text to it without loosing the old text.
app.DebugField.Value = append(app.DebugField.Value, "rew LB\n");
but the newlines are showing up as text \n
Start Trials at 22:26:09 >> rew LB\n
I guess I have to sprintf() the string first but why can't \n be properly interpreted by string handling functions?
  댓글 수: 3
Gavin
Gavin 2024년 8월 1일
What! even sprintf() to magically turn \n into hex13 hex 10 isn't working!
toPicoStr = sprintf(">> %s \n", toPicoStr); % add cmd tag and newline
% WARNING strcat() removes whitespace use append()
app.DebugField.Value = append(app.DebugField.Value, toPicoStr);
gives me
Start Trials at 22:45:32 >>
So it's really crappy when it wraps text like this randomly
Start Trials at 22:53:10 >> R0E0N0S1U150Y200Z500L5>> I2396OPA5C0G >> rew LA 22:56:38
should be
Start Trials at 22:53:10
>> R0E0N0S1U150Y200Z500L5
>> I2396OPA5C0G
>> rew LA 22:56:38
Stephen23
Stephen23 2024년 8월 1일
편집: Stephen23 2024년 8월 1일
"I guess I have to sprintf() the string first but why can't \n be properly interpreted by string handling functions?"
Only the functions which are documented to interpret such special characters (e.g. SPRINTF, FPRINTF, COMPOSE, etc.) will interpret such escaped sequences. Text operations such as concatenation do not interpret anything.
"... even sprintf() to magically turn \n into hex13 hex 10 isn't working!"
Note that carriage return is actually decimal 13 (hex: D) and line feed is decimal 10 (hex: A).
If you want a carriage return followed by a line feed then you can specify this:
txt = sprintf('%s\r\n','hello','world')
txt =
'hello world '
double(txt) % check that char(13) and char(10) have been created
ans = 1x14
104 101 108 108 111 13 10 119 111 114 108 100 13 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Voss advised that an EDIT FIELD is for a single line of text. Use a TEXT AREA if you want to display multiple lines:

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

채택된 답변

SACHIN KHANDELWAL
SACHIN KHANDELWAL 2024년 8월 1일
Hi @Gavin,
I had a quick look into the code and found that the "append" function only takes strings as input and concatenates them. It will not evaluate expressions like "\n".
Therefore, I would recommend trying a different workflow. Here is one that I think might be helpful to you. Please refer to the following MATLAB code:
newText = 'rew LB';
app.TextArea.Value = [app.TextArea.Value; newText]; % This way you can append text in the new line
Hope it helps!
  댓글 수: 1
Gavin
Gavin 2024년 8월 6일
Yes, thanks I happened to figure that out and it works perfectly

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

추가 답변 (2개)

Voss
Voss 2024년 8월 1일
From here:
"If you want to allow multiple lines of text, use a text area component instead of an edit field."

Walter Roberson
Walter Roberson 2024년 8월 6일
app.DebugField.Value = append(app.DebugField.Value, "rew LB" + newline);

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by