- 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!