convert number to string but keep it the same as it is

Hi,
I have a Matrix containing different time series and I gave them numbers (2000001, 2000002,2000003 etc.) and I would like to convert this numbers into a string. If I use int2Str I get some Kind of matlab representation of this number, but this is not what I am looking for. What I need is at the end a vector as follows:
A=['2000001','2000002','2000003', etc.]
how can I achieve this by a automatic command?
regards

 채택된 답변

Star Strider
Star Strider 2014년 9월 19일
In one line:
M = [2000001, 2000002,2000003];
S = strsplit(num2str(M));
produces:
S =
'2000001' '2000002' '2000003'

댓글 수: 4

Locks
Locks 2014년 9월 19일
편집: Locks 2014년 9월 19일
I get the error: Undefined function 'strsplit' for input arguments of type 'char'.
is it possible that strsplit is from a toolbox? i have matlab version 2012, is it possible that this version does not contain that function?
I didn’t see the semicolons that indicated you want them as a column vector of strings.
This works:
M = [2000001, 2000002, 2000003];
S = strsplit(num2str(M))'
and produces:
S =
'2000001'
'2000002'
'2000003'
If ‘M’ was initially either a row vector or a column vector, this works:
M = [2000001, 2000002, 2000003]';
S = strsplit(num2str(M(:)'))'
producing the same column vector as before.
Either num2str or int2str will work in this application.
strsplit (and strjoin) were introduced in version 2013a, if I recall.
I don’t remember when it arrived.
In its absence, this is as good as it gets:
S = cellstr(int2str(M(:)))
and produces:
S =
'2000001'
'2000002'
'2000003'

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

추가 답변 (2개)

Youssef  Khmou
Youssef Khmou 2014년 9월 19일
편집: Youssef Khmou 2014년 9월 19일
You can achieve that command using transformation from matrix to cells, if M is your matrix :
M=magic(10);
N=size(M);
B=mat2cell(num2str(M(:)),ones(N(1)*N(2),1));
B=reshape(B,N(1),N(2));
Mikhail
Mikhail 2014년 9월 19일

0 개 추천

You can convert each number separetely in the for loop.
for i=1:numel(M)
out(:,i)=int2Str(M(i))
end
And you will have out - array of strings Code may have some errors - i didn't try to compile it

댓글 수: 1

tis results in
222
000
000
000
000
000
123
but that is exactly what I whant to avoid

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2014년 9월 19일

댓글:

2014년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by