How do I find the number of character in a text file?
이전 댓글 표시
I have a text file 'inp.txt' containing one line like this:
'banerjee.raktim 123456789125'
I want to know the number of character present in this line, including the white space. And also want to know the position of the white space character.
The expected no of character is 28 and opsition of space is:
16
How to do this?
채택된 답변
추가 답변 (2개)
Walter Roberson
2011년 1월 26일
Are the quote marks and the period present in the file? For the purposes of this discussion I will presume they are not.
a = textread('inp.txt','%s','Whitespace','');
line_length = length(a{1});
space_pos = find(a{1} == ' ',1,'first');
fprintf('The actual number of characters was %d and the actual space position was %d', line_length, space_pos);
댓글 수: 5
raktim banerjee
2011년 1월 26일
raktim banerjee
2011년 1월 26일
raktim banerjee
2011년 1월 26일
Walter Roberson
2011년 1월 26일
Matlab 6.5? Sorry, that is before my time for functions such as textread(). This answer will not work for you.
Jan
2011년 1월 26일
TEXTREAD is available in Matlab 6.5, but you have to use lower case for 'whitespace'.
Gary
2011년 1월 26일
0 개 추천
for a text file, doesn't each character represent 1 byte?
Does the 'dir' function exist in Matlab 6.5? In my tests creating a text file in Windows notepad, then getting the file size using Matlab's 'dir', the reported number of bytes in the file is equal to the number of characters I typed into it.
Of course, that doesn't tell you where the spaces are.
댓글 수: 6
Jan
2011년 1월 28일
DIR existed in Matlab 6.5 and is the best choice to measure the file length.
Walter Roberson
2011년 1월 28일
For text files in MS Windows, line endings take two characters to represent.
For text files in any of the other operating systems other than VMS that Matlab has run on, line terminators are a single character.
I do not know how far back the support for Matlab on VMS went. In my days, VMS commonly used record-oriented files, with end of line terminators being implicit by the length of the record.
Walter Roberson
2011년 1월 28일
Oh yes, what I had meant to say was that file size is not necessarily the same as the number of characters you would read in for the file.
Jan
2011년 1월 28일
The OP wants "the number of characters". Of course he has to consider CHAR[13,10] for DOS line breaks - on demand. And even control characters as CHAR(8) ("backspace") might be counted differently, and multi-byte characters, and CHAR(26) as EOF signal. The number of characters can differ if you open the file in text or binary mode. Although "number of characters" is not well defined, DIR and LENGTH(FGETL) might answer the OPs question suffiently.
Walter Roberson
2011년 1월 28일
The OP wants the number of characters on the first line, and gave a specific expected count that did not include any line terminators.
We saw in an earlier cssm thread that Matlab does not read past ^Z on MS Windows for a file opened as text, so any check based upon file size returned by dir() would have to account for possible line terminators and possible ^Z . I don't think it worth the effort compared to just reading the line and taking its length().
Walter Roberson
2011년 1월 28일
Gary, MS Windows is happy to write UTF-16 text files, so we cannot count on one character being the same as 1 byte, even without these issues of line or file terminators.
카테고리
도움말 센터 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!