Convert a fixed width char array into a column vector

조회 수: 8 (최근 30일)
Adri
Adri 2020년 7월 28일
댓글: Adri 2020년 7월 29일
Hello! I have troubles converting numbers from a char array to double format. The char array always has the dimensions 1x48. Every 8 characters represent an integer. Sometimes there are spaces between numbers and sscanf fails to give the results I need. For example:
aa = ' 1703434 42 1012275140184521401845314018473';
sscanf(aa,'%8d')
ans =
1703434
42
10122751
40184521
40184531
4018473
should return in fact
ans =
1703434
42
10122751
14018452
14018453
14018473
The reason why I don't use str2double for each 8 characters in 'aa' is that it gets very slow for many conversions. The original file I read also contains other words, and this array 'aa' is only a part of each line I read. So far sscanf was the fastest method and it worked well until this line was encountered.
Could anyone please explain me why sscanf behaves like this? Is there any quick alternative?

채택된 답변

David Hill
David Hill 2020년 7월 28일
str2num(reshape(aa,8,[])');
  댓글 수: 1
Adri
Adri 2020년 7월 28일
편집: Adri 2020년 7월 28일
I still used sscanf using the reshape idea of yours. It's a little bit faster:
aa = ' 1703434 1012275140184521401845314018473';
tic
for ii = 1:500000
colvec = sscanf([reshape(aa,8,[])', repmat(' ',length(aa)/8,1)]','%8f ');
end
toc
tic
for ii = 1:500000
colvec2 = str2num(reshape(aa,8,[])');
end
toc
isequal(colvec,colvec2)
Elapsed time is 5.666218 seconds.
Elapsed time is 9.619199 seconds.
ans =
logical
1
Thank you for your suggestion!

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

추가 답변 (2개)

Hamed Moasses
Hamed Moasses 2020년 7월 28일
Hi !
This Answer may help you.
bests

Stephen23
Stephen23 2020년 7월 28일
편집: Stephen23 2020년 7월 28일
This will be quite efficient:
>> aa = ' 1703434 42 1012275140184521401845314018473';
>> vec = sscanf(sprintf('%c%c%c%c%c%c%c%c,',aa),'%f%*[ ,]')
vec =
1703434
42
1012275
14018452
14018453
14018473
  댓글 수: 2
Stephen23
Stephen23 2020년 7월 28일
Timing for 1e4 iterations:
Elapsed time is 2.80135 seconds. % reshape, repmat, sscanf
Elapsed time is 7.16556 seconds. % reshape, str2num
Elapsed time is 0.92013 seconds. % this answer
Adri
Adri 2020년 7월 29일
I tried this alternative both in my main code and in this example and it seems that on my machine it's slower (weird or not?):
aa = ' 1703434 1012275140184521401845314018473';
tic
for ii = 1:500000
colvec = sscanf([reshape(aa,8,[])', repmat(' ',length(aa)/8,1)]','%8f ');
end
toc
tic
for ii = 1:500000
colvec2 = str2num(reshape(aa,8,[])');
end
toc
tic
for ii = 1:500000
colvec3 = sscanf(sprintf('%c%c%c%c%c%c%c%c,',aa),'%f%*[ ,]');
end
toc
Elapsed time is 5.695740 seconds.
Elapsed time is 9.565251 seconds.
Elapsed time is 15.707137 seconds.
Anyway, thank you very much for your suggestion!

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by