Opening csv file using textscan returns [0x1 double]

1 visualizzazione (ultimi 30 giorni)
I originally had a code that opened text files so that I could read the data: here is an example data file:
Frequency/Hz ZRe/Ohm ZIm/Ohm
25000 123.973 -55.621
22471.91 130.936 -58.299
20000 131.607 -61.111
17977.53 132.36 -66.828
16064.26 134.676 -70.063
14466.55 137.25 -75.438
12841.09 138.59 -82.377
11494.25 141.42 -83.387
10335.92 142.741 -96.216
9195.4 151.153 -100.342
8205.13 146.893 -111.375
7326.01 152.058 -121.697
6568.14 157.189 -133.012
5882.35 160.671 -145.992
5259.7 163.611 -160.421
4705.88 166.562 -176.182
4203.89 169.427 -193.571
3766.48 170.511 -212.774
3361.34 173.806 -235.36
3013.18 179.252 -258.61
and the beginning of the code:
function [zizr]= getdata3(filename)
currentfolder=pwd
PathStr=pwd;
textFiles=dir([PathStr '*.txt'])
MyData=zeros(100,22); %assume data has 100 rows
for k = 1:3;
length(textFiles)
textFilename = [filename num2str(k) '.txt']
%Open text file, 'rt' is for reading a text file only
fid=fopen(textFilename, 'rt')
%Read file into 3 seperate variables
Data = textscan(fid,'%f %f %f','Headerlines',1)
that code works fine, however now I would like to open csv files that have 5 headerlines:
Type:,FixedPotential
Date and time:,2015-09-02 08:56:49
Title:,CH1-090215SynthUrine-3
series:,0.060,0.029
freq / Hz,Z' / Ohm,Z'' / Ohm,
25000.00,123.973,-55.621,
22471.91,130.936,-58.299,
20000.00,131.607,-61.111,
17977.53,132.360,-66.828,
16064.26,134.676,-70.063,
14466.55,137.250,-75.438,
12841.09,138.590,-82.377,
11494.25,141.420,-83.387,
10335.92,142.741,-96.216,
9195.40,151.153,-100.342,
8205.13,146.893,-111.375,
7326.01,152.058,-121.697,
6568.14,157.189,-133.012,
5882.35,160.671,-145.992,
5259.70,163.611,-160.421,
4705.88,166.562,-176.182,
4203.89,169.427,-193.571,
3766.48,170.511,-212.774,
3361.34,173.806,-235.360,
3013.18,179.252,-258.610,
I would like to do the exact same thing as in my previous code, just with csv files. Here is what i have but it doesnt seem to work.
function [zizr]= getdata3(filename)
currentfolder=pwd
PathStr=pwd;
textFiles=dir([PathStr '*.csv'])
MyData=zeros(100,22); %assume data has 100 rows
for k = 1:3;
length(textFiles)
textFilename = [filename num2str(k) '.csv']
%Open text file, 'rt' is for reading a text file only
%fid=fopen(textFilename, 'r')
fid=fopen(textFilename,'rt')
%Read file into 3 seperate variables
Data = textscan(fid,'%f %f %f','Delimiter',',','headerlines',5)
I keep getting : Data =
[0x1 double] [0x1 double] [0x1 double]
I am mainly concerned with the textscan inputs I have and if something is wrong there.
  7 Commenti
Walter Roberson
Walter Roberson il 4 Set 2015
If the encoding of the file is a problem try
fid=fopen('090415_D2_PBS-1.csv','rt');
If that is not enough then you an try
fid=fopen('090415_D2_PBS-1.csv','rt', 'ieee-le', 'UCS-2');
and if it complains about UCS-2 not being known, try 'UTF-16' instead (UTF-16 expands on UCS-2 to allow additional code points)
Alano Ogata
Alano Ogata il 16 Set 2015
Modificato: Walter Roberson il 16 Set 2015
Thanks for all the help! it was an encoding problem.
fid=fopen('090415_D2_PBS-1.csv','rt', 'ieee-le', 'UCS-2');
fixed the problem.
Thanks

Accedi per commentare.

Risposte (2)

per isakson
per isakson il 4 Set 2015
Modificato: per isakson il 5 Set 2015
A bit of googling in combination with trial and error
>> cac = cssm('090315_D1_20BSA-3.csv')
Warning: The encoding 'UTF-16LE' is not supported.
See the documentation for FOPEN.
> In cssm at 2
cac =
[20x1 double] [20x1 double] [20x1 double]
where
function zizr = cssm( filespec )
fid = fopen( filespec, 'r', 'l', 'UTF16-LE' );
str = fread( fid, '*char' )';
fclose( fid );
zizr = textscan( str(3:end), '%f%f%f', 'Delimiter', ',', 'headerlines', 5 );
end
&nbsp
*This should work, but it doesn't :-(* &nbsp (Can anyone explain why?)
>> cac = cssm('090315_D1_20BSA-3.csv')
Warning: The encoding 'UTF-16' is not supported.
See the documentation for FOPEN.
> In cssm at 2
Warning: The encoding 'UTF-16' is not supported.
See the documentation for FOPEN.
> In cssm at 4
cac =
[0x1 double] [0x1 double] [0x1 double]
where
function zizr = cssm( filespec )
fid = fopen( filespec, 'rt', 'ieee-le', 'UCS-2');
fseek( fid, 2, 'bof' ); % skip BOM-character
zizr = textscan( fid, '%f%f%f', 'Delimiter', ',', 'headerlines', 5 );
fclose( fid );
end
fread honors the encoding settings, but textscan doesn't ???

shannon stoffel
shannon stoffel il 2 Set 2015
Modificato: per isakson il 4 Set 2015
[filename1,filepath1]=uigetfile({'*.*','All Files'}, 'Select Data File 1');
cd(filepath1);
rawdata1=dlmread(filename1);
% Opens the file
M = csvread(filename1,1,1);
  3 Commenti
Walter Roberson
Walter Roberson il 4 Set 2015
I noticed recently that dlmread() now has an example of a non-numeric header.
dpb
dpb il 4 Set 2015
Hmmmm....so the doc now says. Besides the "Introduced" date, such major functionality changes should also be in the footnotes as to when it is introduced (or vice versa, when removed as sometimes also happens).

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by