How to import csv files in Matlab?
4 views (last 30 days)
Show older comments
I would like to import data from csv files from the folder. I used to "import data", but there is no possibility to import more than one file. So I want to remodel script which was generated by "import data". I have about 10-15 csv files in the folder. How can I do this? Give me any advice on importing the CSV files. Thank you. There is script generated by Matlab:
%%Initialize variables.
filename = 'C:\...\laser 1\1km\TRACE904.CSV';
delimiter = ',';
startRow = 14;
%%Format string for each line of text:
formatSpec = '%f%f%*s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Create output variable
TRACE904 = [dataArray{1:end-1}];
%%Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
0 Comments
Answers (1)
Stephen23
on 14 Nov 2015
Edited: Stephen23
on 14 Nov 2015
Reading multiple files is such a common topic that it is covered very well on our Wiki:
And currently there are more than seven hundred threads on this forum when I search for "read CSV". Did you read any of them?:
Also the documentation contains lots of hints and advice, which you can browse using the contents panel:
0 Comments
See Also
Categories
Find more on MATLAB Compiler 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!