0
votes

I am using csvread syntax m =csvread('reserve2.csv',7,3,[7,3,9,4]) from Matlab to read comma seperated values from a CSV file. Unfortunately the numbers in the specified rows and columns in the CSV file are listed with double quotation marks around them and I get the following error:

Error using dlmread (line 143) Mismatch between file and format string. Trouble reading 'Numeric' field from file (row number 1, field number 4) ==> "0","568"\n

Error in csvread (line 49) m=dlmread(filename, ',', r, c, rng);

How can I invoke csvread such that it can read the values even when they are in double quotation marks? Or how can I write a code to get rid of the quotation marks in the CSV file?

1

1 Answers

0
votes

There are several ways of reading in this file.

  1. MATLAB's TEXTSCAN function can parse text and ignore delimiters enclosed within double quotation marks (" "). Use the TEXTSCAN function with the '%q' format type to identify strings demarcated by double quotation marks. For example:

str = 'a,A,"a,apple"'; out=textscan(str,'%s%s%q', 'delimiter',',') This command will generate a cell array 'out' that contains 'a', 'A' and 'a,apple'.

  1. If you are on a Windows platform and have Microsoft Excel installed, you can use the following syntax with XLSREAD to read your data into two cell arrays:

    [num_data text_data] = xlsread(filename);

After executing this command, the data will be copied to 2 different arrays that treat the data differently:

"num_data" - containing numeric data only; strings and empty fields will be converted to NaN

"text_data" - containing all data read in as strings. The text between two double quotes will be parsed as a single string

  1. Create a custom function to parse the file using multiple FREAD or FGETL commands.

For more information on these functions, refer to the documentation by executing the following at the MATLAB command prompt:

doc textscan doc xlsread doc fread doc fgetl