- read the file as characters
- remove all '"'
- parse the character string with textscan
Importing from Openoffice
조회 수: 1 (최근 30일)
이전 댓글 표시
The csv (or xls) file is a list of numbers but when i import it shows a list of numbers in quotes "234" "432" etc
and im not able to import them as numbers. Only way is to import as a cell array but then i cant use them as numbers in matlab and i cant convert them to numbers.
댓글 수: 0
답변 (5개)
per isakson
2012년 6월 7일
CSV comes in different flavors and Microsoft make their own rules. Google "CSV file format" and see for example Common Format and MIME Type for Comma-Separated Values (CSV) Files. I guess OpenOffice tries to honor the "standard".
It seems Matlab has no obvious way to read proper CSV files.
A brute approach is to
Something like
function M = Answer( )
fid = fopen( 'cssm.txt', 'r' );
str = fread( fid, '*char' );
sts = fclose( fid );
str( str == '"' ) = [];
cac = textscan( str, format );
peel off the braces
end
This will certainly not work in all cases
CapaB
2012년 6월 8일
댓글 수: 2
per isakson
2012년 6월 8일
It's hard to guess what's going on! What does str(1:120)' display?
"(Error using fread Invalid file identifier.)" hints that you fail to open the file. However, you say you read "4630x1 cell".
Why use cell2mat?
per isakson
2012년 6월 9일
.
"commas instead of dots" is definately a problem!
fid = fopen( 'HMhist.csv', 'r' );
str = fread( fid, '*char' );
sts = fclose( fid );
str( str == '"' ) = [];
str( str == ',' ) = '.';
cac = textscan( str, '%f' );
Why do you believe that the format, '%f', is appropriate? First you need to get the format right.
What does str(1:120)' display? Don't forget the blip! Or do
permute( str(1:124), [2,1] )
CapaB
2012년 6월 10일
댓글 수: 1
Walter Roberson
2012년 6월 10일
I would suggest this should be a new Question as it has nothing to do with importing CVS
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!