Inserting a tab character into a string
조회 수: 492 (최근 30일)
이전 댓글 표시
I am attempting to insert a space into a string. Here is my code:
t1 = 'Control Chart For ';
t2 = '\t';
t3 = num2str(w);
t4 = ' water flow ';
t5 = '\t';
t6 = num2str(o);
t7 = ' oil flow ';
str = strcat(t1, t2, t3, t4, t5, t6, t7);
This should yield:
Control Chart For 5 water flow 3 oil flow
however it yields instead:
Control Chart For\t5 water flow\t3 oil flow
Just adding spaces in the string does not work either and making the string \t instead of '\t' also fails.
댓글 수: 0
답변 (5개)
Andrew Newell
2011년 2월 3일
A nicer way of doing it is
str = sprintf('Control Chart For \t %d water flow \t %d oil flow',w,o)
댓글 수: 2
Oleg Komarov
2011년 2월 3일
I would go for this approach, it is more readable the way space chars are handled.
Paulo Silva
2011년 2월 3일
char(9) instead of only one string doesn't seem to work, I also tried, sprintf('\t'), this way works:
t1 = 'Control Chart For ';
t3 = [char(9) num2str(w)];
t4 = ' water flow ';
t6 = [char(9) num2str(o)];
t7 = ' oil flow ';
str = strcat(t1,t3, t4, t6, t7)
댓글 수: 3
the cyclist
2011년 2월 3일
Are you trying to insert a tab character, or just white space? If the latter, you can concatenate with square brackets:
>> str = [t1, t3, t4, t6, t7]
Then the trailing whitespace in t1, etc, will not be dropped, and you don't need to manually insert the space.
Looks like this syntax works for tab as well:
>> str = [t1, char(9), t2, t3, t4, t5, t6, t7]
댓글 수: 0
fernando bordignon
2016년 3월 23일
You can also use the tab itself, but you need to disable the "insert white spaces" option at the editor preferences. Another way of doing that would be by copy pasting the tab character from another editor.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!