csvread has problems reading all columns

5 views (last 30 days)
Hi together
I'm trying to import a .csv file which was generated during a sensor measuring. The problem is that the last seven columns are simply not read.. The array I get has 89 columns, but the .csv file should have 96 columns.
when I write: sensor_signal=csvread('Test2.csv',1,0,[1,0,500,95]);
I'm getting the error message: _ _Index in position 2 exceeds array bounds (must not exceed 89).
Error in dlmread (line 159) result= result(:,1:ncols);
Error in csvread (line 50) m=dlmread(filename, ',', r, c, rng);
Error in NSensoreneinlesen (line 7) sensor_signal=csvread('Test2.csv',1,0,[1,0,500,95]);__
If i try to import the file as following: sensor_signal=csvread('Test2.csv',1,0) I'm getting an array with 89 columns
I'm absolutely sure that the file contains 96 values per row.
If I copy a row of the data manually into Matlab, I'm getting a 1x96 vector.
"2,00032,343,64.0,64.0,0.0,0.0,0.0,90.0,-4.589,-4.547,-1.257 5,00031,703,104.0,112.0,0.0,0.0,0.0,90.0,3.743,3.093,-24.776 1,00029,967,104.0,112.0,40.0,0.0,0.0,90.0,11.453,4.799,19.710 3,00026,1281,64.0,64.0,40.0,0.0,0.0,90.0,-6.930,2.304,11.907 6,00025,1651,64.0,112.0,0.0,0.0,0.0,90.0,1.972,1.425,10.804 4,00028,2105,64.0,112.0,40.0,0.0,0.0,90.0,7.458,3.738,-16.818 8,00034,2564,104.0,64.0,0.0,0.0,0.0,90.0,-6.402,-2.958,3.346 7,00030,2919,104.0,64.0,40.0,0.0,0.0,90.0,-5.126,10.827,-12.724 "
When I copy the row from the .csv file and paste it, it has these " at beginning and at the end. could this probably be the problem?
Hope someone can help me with this task...
  3 Comments
Stephen23
Stephen23 on 9 May 2018
Edited: Stephen23 on 9 May 2018
@Rrahim Gashi: that file has some features that make it hard to read:
  • mixed delimiters: tabs and commas.
  • double quotes at the start and end of each line.
These unfortunate features mean that the file is not really a CSV file at all (except if you consider it to be a CVS file with one column of double-quote delimited strings: but you wrote that you believe that the file has multiple columns, so this means that the file has simply been written very badly). The best solution would be to fix whatever tool writes that file, so that it does not use double quotes at the ends of the lines and so that it only uses commas as the delimiter. Then you could read the file trivially using csvread. Otherwise see my answer.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 9 May 2018
Edited: Stephen23 on 9 May 2018
This will read your non-standard CSV file (attached to this answer) into matrix M:
fmt = repmat('%f',1,98); % two extra columns for the " characters.
opt = {'Delimiter','\t,','CollectOutput',true,'TreatAsEmpty','"'};
[fid,msg] = fopen('Test.csv','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
M = C{1}(:,2:97); % remove " columns
and checking the output:
>> size(M)
ans =
3 96
>> M(:,1:9) % first eight columns
ans =
2 32 343 64 64 0 0 0 90
2 32 343 64 64 0 0 0 90
2 32 343 64 64 0 0 0 90
>> M(:,end-7:end) % last eight columns
ans =
64.00000 40.00000 0.00000 0.00000 90.00000 -5.12600 10.82700 -12.72400
64.00000 40.00000 0.00000 0.00000 90.00000 -5.12600 10.82700 -12.72400
64.00000 40.00000 0.00000 0.00000 90.00000 -5.12600 10.82700 -12.72400

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!