Read numeric data with csvread
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello,
I got a csv-file that looks like this.
* text here
* more text...
1,20,3,4
2,30,4,5
* text again
3,4,6,7
*text
And so it goes on.
How do I read the csv-file and only get the numeric data. Everything that has a "*" and text after should be disgarded.
Thank you.
채택된 답변
dpb
2015년 2월 12일
doc textscan % NB: optional 'commentstyle' parameter
댓글 수: 8
Okey. I created TestFile.csv with the data and text in as in my question.
Now my code is:
fileID=fopen('TestFile.csv')
N=4
cdata=textscan(fileID,'%f %f %f %f', ...
N,'CollectOutput',1,'CommentStyle','*')
I get:
cdata =
[1x4 double]
I cant figure out how to get the data from each column in "cdata"?
Thank you.
dpb
2015년 2월 12일
For these cases where there's no need for a cell array at all I wrap textscan in cell2mat as--
cdata=cell2mat(textscan(fileID,'%f %f %f %f', ...
N,'CollectOutput',1,'CommentStyle','*'));
In general you dereference a cell array with the "curlies" as
cdata(:)
for the full array or "nested indexing" of
cdata(1){r,c)
for a given array element.
See the doc on cell arrays for the fuller details.
But the short story here is that there's no need for the cell arrray and it's unfortunate there's not a way to tell textscan to forego the needless creation of one when isn't needed.
Thank you! My cdata looks like below when I use cell2mat:
cdata =
1 NaN NaN NaN
"1" is from row 1 and column 1 in my TestFile.csv I thought that it could be a bad csv-file but I tried to open other files to but it gives the same answer.
Am I using the wrong formatSpec?
dpb
2015년 2월 17일
Dunno...you don't show what you did in context...w/ the sample file copied into a text file here the example worked fine. NaN indicates a conversion of something not recognizable as a number so perhaps there's an embedded hidden character in the file or somesuch???
Daniel
2015년 2월 18일
Okey. There should not be andy hidden characters in the file. That is confirmed.
This is my script:
---
fileID=fopen('TestFile.csv')
N=4
cdata=cell2mat(textscan(fileID,'%f %f %f %f',N,'CollectOutput',1,'CommentStyle','*'))
---
And this is the result from Matlab:
---
fileID = 8
N = 4
cdata = 1 NaN NaN NaN
---
And you have the exact same thing and it works for you? That is strange.
Thanks anyway!
Ayup...
>> type test.csv
* text here
* more text...
1,20,3,4
2,30,4,5
* text again
3,4,6,7
*text
>> fid=fopen('test.csv');
>> cell2mat(textscan(fid,repmat('%f',1,4),'delimiter',',', ...
'commentstyle','*', ...
'collectoutput',1))
ans =
1 20 3 4
2 30 4 5
3 4 6 7
>>
ADDENDUM
Oh, I see it isn't exact same thing; you don't need/want the repeat count specifier. That tells it to apply the format string N times but your file isn't consistent so it breaks when finds a non-numeric form. It would possibly work that way if 'commentstyle' were to force the whole file to be processed, the comment lines removed, then that file processed, but textscan works sequentially, not globally, simply skipping a line beginning with the comment character when it finds one and trying to convert the next line.
Thank you for your help! It works fine now. So if I had five columns instead of four i would write "1,5". Now I get how it works.
dpb
2015년 2월 20일
Ayup; it's the silly way C implemented it's format strings ignoring the long-existing pattern used in Fortran wherein there can be a repeat specifier. Just to show they were smarter; the implementers reversed the order of the width field and the conversion type so there's no way to now write a repeat count unambiguously. In Fortran FORMAT it would be 4F8.0; in Matlab which uses C i/o libraries one has to use repmat to double up or write them all explicitly. On the newsgroup am working with a guy at this instant with a 159-column file...writing %f 159 separate times is rather painful as his initial plea noted until one either has the "a-ha!" moment one's self or somebody shows you the trick (S Lord pointed it out to me years ago; I had never thought of repmat for strings for the purpose despite complaining for years. At one time I wrote a mex file that accepted Fortran FORMAT strings and used the Fortran i/o and passed the values back. Unfortunately I lost the source in the retirement move and haven't had the gumption to re-invent it since.
OK, enough geezer stories/griping... :)
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
태그
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
