Error importing .csv file with import script generated but not with green tick import button

5 views (last 30 days)
I am trying to open a .csv file, and it is imported when I use the gree tick button 'import selection', but as soon as I generate the script to import it and run the script, it gives me the following error:
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> coords,x,y,likelihood,x,y,likelihood,x,y,likelihood\n
Here is the script generated by MATLAB to import the .csv as a numeric matrix:
%% Import data from text file.
% Script for importing data from the following text file:
%
% V:\Tanika\Rig4_preprocessing\VIP\34\210226\DLC\ConcatenatedCsv.csv
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2022/05/23 16:00:43
%% Initialize variables.
filename = 'V:\Tanika\Rig4_preprocessing\VIP\34\210226\DLC\ConcatenatedCsv.csv';
delimiter = ',';
startRow = 4;
%% Format for each line of text:
% column1: double (%f)
% column2: double (%f)
% column3: double (%f)
% column4: double (%f)
% column5: double (%f)
% column6: double (%f)
% column7: double (%f)
% column8: double (%f)
% column9: double (%f)
% column10: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%f%f%f%f%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%% Create output variable
ConcatenatedCsv = [dataArray{1:end-1}];
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
I have attached the file I'm trying to import. For some reason if you run the line starting with 'dataArray' twice, it works the second time, but removes my first line.
Really appreciate any help thanks!! =)

Accepted Answer

Voss
Voss on 23 May 2022
Edited: Voss on 23 May 2022
The problem is that the line endings in that file are \r\r\n, i.e., two carriage return characters followed by a line feed character.
One way to get around this is to replace those line endings with one that textscan can handle. Here I replace them with a single line feed character:
% you won't need to unzip the zip file, this is just to run it here:
unzip('ConcatenatedCsv.zip')
% open, read, and close the file:
fileID = fopen('ConcatenatedCsv.csv');
data = char(fread(fileID).');
fclose(fileID);
% replace \r\r\n with \n in data:
data = strrep(data,char([13 13 10]),newline());
% now do textscan as before, but on the text
% itself (data) rather than the file handle fileID:
delimiter = ',';
startRow = 4;
formatSpec = '%f%f%f%f%f%f%f%f%f%f%[^\r\n]';
dataArray = textscan(data, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
ConcatenatedCsv = [dataArray{1:end-1}];
% check the size:
size(ConcatenatedCsv)
ans = 1×2
54150 10
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!