strcat two char arrays, how to remove the space?

I have two char arrays a and b, say
a is a 5*14 char
var =
NKY 08/12/11 C
NKY 08/12/11 C
NKY 08/12/11 C
NKY 08/12/11 C
NKY 08/12/11 C
b is a 5*5 char
val =
5900
5905
5910
5915
5920
If I do strcat(a,b) or [a,b], it gives me
val =
NKY 08/12/11 C 5900
NKY 08/12/11 C 5905
NKY 08/12/11 C 5910
NKY 08/12/11 C 5915
NKY 08/12/11 C 5920
but I need
val =
NKY 08/12/11 C5900
NKY 08/12/11 C5905
NKY 08/12/11 C5910
NKY 08/12/11 C5915
NKY 08/12/11 C5920
Could anyone tell me how to do that? Very appreciated!

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 7월 15일

1 개 추천

val(:,15) = []; %just delete that column

댓글 수: 3

Zoe Zhang
Zoe Zhang 2011년 7월 15일
Thanks! So there is like no one-step solution by just manipulating strcat, right?
the problem is that your "5x5" char, the second input to strcat, is actually a 6x5 char, I believe. It has nothing to do with strcat - it's not adding the zero.
Zoe Zhang
Zoe Zhang 2011년 7월 15일
Thanks again.
Btw, my b is from num2str of a vector of number.

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 7월 15일

1 개 추천

You either have trailing white space at the end of "a" or leading whitespace at the begining of "b". check out the following functions.
help strtrim
help deblank

댓글 수: 4

Zoe Zhang
Zoe Zhang 2011년 7월 15일
Thank you!
strcat(strtrim(a),strtrim(b));
and strcat(deblank(a),deblank(b));
both gives me the 'NKY 08/12/11 C 5900'
actually my a is coming from repmat, b is from num2str
Well, look at your b, it has 4 letters per row but it says 5x5 char so it got be the leading whitespace.
>> b=[' 5900';' 5905';' 5910';' 5915';' 5920'];
>> c=strtrim(b);
>> whos
Name Size Bytes Class Attributes
b 5x5 50 char
c 5x4 40 char
Zoe Zhang
Zoe Zhang 2011년 7월 15일
IC, thanks :)
The reason might be that you only showed a small portion of your data.
a=[5900:5:5920,15900];
b=num2str(a')
b =
5900
5905
5910
5915
5920
15900

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

Walter Roberson
Walter Roberson 2011년 7월 15일

1 개 추천

strcat('NKY 08/12/11 C', strtrim(cellstr(b)))
Note that this allows you to skip the repmat of the constant string.
Note also that this will return a cell array of strings; you can use char() to convert back to a char array.

댓글 수: 1

Zoe Zhang
Zoe Zhang 2011년 7월 15일
Neat and efficient. And the space problem disappear. Thanks a lot!

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by