Clear Filters
Clear Filters

please help me read a csv file

6 views (last 30 days)
i cannot read files using csvread or dlmread
  1 Comment
Jan on 22 Apr 2014
"Cannot" is not a sufficient description of the occurring problems. Please post your code and show us the error messages or explain the difference between your expectations and the results.

Sign in to comment.

Accepted Answer

Friedrich on 22 Apr 2014
maybe consider using
and let MATLAB do the job for you. You can generate M-Code for reading in the data, e.g.
%%Import data from text file.
% Script for importing data from the following text file:
% C:\Users\fhempel\Desktop\tmp\01012013.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 2014/04/22 11:37:26
%%Initialize variables.
filename = 'C:\Users\fhempel\Desktop\tmp\01012013.CSV';
delimiter = ',';
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% 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, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
%%Close the text file.
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,22,23,27]
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
catch me
%%Split data into numeric and cell columns.
rawNumericColumns = raw(:, [3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,22,23,27]);
rawCellColumns = raw(:, [1,2,19,20,21,24,25,26]);
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns); % Find non-numeric cells
rawNumericColumns(R) = {NaN}; % Replace non-numeric cells
%%Allocate imported array to column variable names
DATE = rawCellColumns(:, 1);
TIME = rawCellColumns(:, 2);
TEMP = cell2mat(rawNumericColumns(:, 1));
RH = cell2mat(rawNumericColumns(:, 2));
WDIR = cell2mat(rawNumericColumns(:, 3));
WSPEED = cell2mat(rawNumericColumns(:, 4));
RAINFALL = cell2mat(rawNumericColumns(:, 5));
APRESSURE = cell2mat(rawNumericColumns(:, 6));
SRAD = cell2mat(rawNumericColumns(:, 7));
EVAP = cell2mat(rawNumericColumns(:, 8));
AN1 = cell2mat(rawNumericColumns(:, 9));
AN2 = cell2mat(rawNumericColumns(:, 10));
AN3 = cell2mat(rawNumericColumns(:, 11));
AN4 = cell2mat(rawNumericColumns(:, 12));
DIG1 = cell2mat(rawNumericColumns(:, 13));
DIG2 = cell2mat(rawNumericColumns(:, 14));
DIG3 = cell2mat(rawNumericColumns(:, 15));
DIG4 = cell2mat(rawNumericColumns(:, 16));
PV = rawCellColumns(:, 3);
BAT = rawCellColumns(:, 4);
ACV = rawCellColumns(:, 5);
LAT = cell2mat(rawNumericColumns(:, 17));
LON = cell2mat(rawNumericColumns(:, 18));
ALTI = rawCellColumns(:, 6);
ELEVATION = rawCellColumns(:, 7);
DEVICEINFO = rawCellColumns(:, 8);
CHECKSUM = cell2mat(rawNumericColumns(:, 19));
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns R;

More Answers (2)

Niklas Nylén
Niklas Nylén on 22 Apr 2014
You can also try with importdata

Image Analyst
Image Analyst on 22 Apr 2014
If you have R2013b or later, try readtable:
t = readtable('01012013.csv');
readtable() is able to handle numeric and non-numeric data on the same line.


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

Community Treasure Hunt

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

Start Hunting!