The first character in your file is a semicolon (;) which is also the delimiter and that's making it difficult to import your data.
Option 1: export the data properly
If you can re-export the data properly and avoid the leading semicolon, do that and this method will work. It's better than the second method.
Check the datetime formats I've specified to make sure they match your data's formats.
file = '2020.csv';
opts = delimitedTextImportOptions("NumVariables", 4);
opts.DataLines = [6, Inf];
opts.Delimiter = ";";
opts.VariableNames = ["Datum", "von", "bis", "MW"];
opts.VariableTypes = ["datetime", "datetime", "datetime", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts = setvaropts(opts, "Datum", "InputFormat", "dd.MM.yyyy");
opts = setvaropts(opts, "von", "InputFormat", "HH:mm");
opts = setvaropts(opts, "bis", "InputFormat", "HH:mm");
T = readtable(file, opts);
Look at the first few rows of the data
Option 2: Fix the file in Matlab before importing
If you can't re-export the data properly, fix the files first in Matlab using the method below and then read them in. The first section below read in the file as text, removes null characters, char(0), and then re-writes the data with a new file name so you don't lose the initial data file. The second section then imports the clean file.
See inline comments for important details.
file = '2020.csv';
txt = fileread(file);
txtClean = regexprep(txt,char(0),'');
fid = fopen(['clean_',file],'w');
fprintf(fid, '%s',txtClean);
fclose(fid);
Now you have a new file named "clean_2020.csv" and it can be read in without problems. Check the datetime values to make sure they match your formats.
newfile = ['clean_',file];
opts = delimitedTextImportOptions("NumVariables", 4);
opts.DataLines = [6, Inf];
opts.Delimiter = ";";
opts.VariableNames = ["Datum", "von", "bis", "MW"];
opts.VariableTypes = ["datetime", "datetime", "datetime", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts = setvaropts(opts, "Datum", "InputFormat", "dd.MM.yyyy");
opts = setvaropts(opts, "von", "InputFormat", "HH:mm");
opts = setvaropts(opts, "bis", "InputFormat", "HH:mm");
T = readtable(newfile, opts);
Look at the first few rows of T
3 Comments
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/593539-problem-to-read-csv-file-with-a-blank-line#comment_1008625
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/593539-problem-to-read-csv-file-with-a-blank-line#comment_1008625
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/593539-problem-to-read-csv-file-with-a-blank-line#comment_1008643
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/593539-problem-to-read-csv-file-with-a-blank-line#comment_1008643
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/593539-problem-to-read-csv-file-with-a-blank-line#comment_1008667
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/593539-problem-to-read-csv-file-with-a-blank-line#comment_1008667
Sign in to comment.