Textscan skipping character 'd'

So I have this string:
fileName ='Br0HAA2dSJ_752012_1.gcd'
And I want to use textscan to extract the 0, HAA, 2, and d in separate cells
DepVar =textscan(fileName,'Br %f %[THAMS] %d %c');
but in the last cell (DepVar{1,4}) it returns a 'S' instead of the 'd' that I want. What is going on and how do I fix it?
Thanks,
Sid

 채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 20일

0 개 추천

If I recall correctly, in some versions, a "d" or "D" immediately following an integer may be interpreted as being part of an exponent, just like "e" and "E". Even though the exponent character was not followed by an exponent value (optional sign, and at least one digit), the exponent character was not being backed up over.
There was an interesting manifestation of this about a month ago, in which the code did back up from a 'e' that was not followed by digits, but the library turned it into an 'E' -- that is, the library had recorded that it was in that state but "rebuilt" based upon the state instead of remembering the actual characdter.

댓글 수: 2

Sid
Sid 2012년 7월 20일
Yeah that is what was happening so I just did this:
DepVar = textscan( fileName, 'Br%f%[THAMS]%d%c','expchars','None' );
Since I don't use exponents in my code and it worked fine.
Thanks
Walter Roberson
Walter Roberson 2012년 7월 20일
It might have been sscanf() in that question. I think it would be tough to locate the Question again.
Having "2d0" interpreted as a number even for integer format specifiers is something I consider a bug. I remember that topic arising a few weeks ago.

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

추가 답변 (1개)

per isakson
per isakson 2012년 7월 20일
편집: per isakson 2012년 7월 20일

0 개 추천

Try:
DepVar = textscan( fileName, 'Br%u%[THAMS]%d%c%*s' );
.
--- Response to comment ---
I cannot reproduce the behavior you see, i.e. "S", with textscan (R2012a - the only one I have installed). However,
cac = cell(1,4);
[cac{:}] = strread( file_name, 'Br%f%[THAMS]%f%c%*s' )
cac =
[0] {1x1 cell} [2] 'S'
Note the "S".
strread is a predecessor of textscan (and I guess textscan is partly based on old code used by strread).
I have not checked the bug database. I propose you report this as a bug.
.
--- In response to Walters answer ---
This might work. However, the number of digits must be known and the item is returned as a string and must be converted: str2double.
DepVar = textscan( file_name, 'Br%f%[THAMS]%1[1234567890]%c%*s' )

댓글 수: 4

Sid
Sid 2012년 7월 20일
Still doesn't work and the 0 could also be a floating point later on in the code
per isakson
per isakson 2012년 7월 20일
It returns "0, HAA, 2, and d" with your particular sample string under R2012a. What do you mean by "doesn't work"?
Sid
Sid 2012년 7월 20일
Still gives me an 'S' at the end
Maybe its because I am using 2010b. Maybe this is an issue for older versions only. Can you give me a "backward compatible" answer.
per isakson
per isakson 2012년 7월 20일
Did you consider using REGEXP?

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

카테고리

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

태그

질문:

Sid
2012년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by