How to import csv files in MATLAB?

I'm having trouble importing an entire csv folder into matlab workspace. I created the file path for the numerous csv files but, do not know how to load them into the workspace next in order to filter and plot? Please advise.
myFolder = 'C:\Users\AbrahamCaceres\Documents\My Documents\Clean Data';
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
if myFolder == 0
return;
end
end
filePattern = fullfile(myFolder, '**/*.csv');
theFiles = dir(filePattern);
for k = 1: length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile('c:\','theFiles','matlab',baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Move files into workspace? Then filter & plot data.
end

 Accepted Answer

Cris LaPierre
Cris LaPierre on 16 Nov 2021
Use readtable, readmatrix, or readcell. My preference is readtable.

6 Comments

An error appears when using readtable. The error states unable to find or open 'c:\theFiles\matlab\Refined_1.csv' check the path and filename. Does the path need to be exactly the same as myFolder? I'm wondering where do I go from here?
readtable(fullFileName);
Cris LaPierre
Cris LaPierre on 17 Nov 2021
Edited: Cris LaPierre on 17 Nov 2021
If you are going to use the full file path, then it needs to be exact. The path in your error message does not appear to be what you intended:
'c:\theFiles\matlab\Refined_1.csv'
I think you have a mistake in your code, specifically here:
fullFileName = fullfile('c:\','theFiles','matlab',baseFileName);
% ^^^^^^^^^^
Since you create a variable named theFiles, I suspect that is what you intend to be used to build the file path, and not the literal text 'theFiles'. Remove the quotes to use the variable value. Remeber the output of dir is a structure.
If you did intend to use the path as it's currently created, the error message is telling you the files you are trying to read in do not exist there. In that case, then yes, you must have the same path as myFolder, or more specifically, theFiles(k).Folder, as that is where the file was found.
That didn't work another error appeared. Does it need to be this?
fullFileName = fullfile('C:\Users\AbrahamCaceres\Documents\My Documents\Clean Data',baseFileName);
I can't say for sure, as the file is on your file system, not mine.
To load the file, I would do this:
fullFileName = fullfile(theFiles(k).folder,theFiles(k).name);
data = readtable(fullFileName);
I used the code recommended. Making progress but all the files are combining together into one labeled data. My goal is to load the files seperately instead of as one. I have files labeled Refined1.csv and Raw1.csv and they are being combined together instead of seperate. What I tried below didn't make a difference as well.
fullFileName = fullfile(theFiles(k).folder,theFiles(k).name,filesep)
You need to implement some flow control based on the file name. See Ch 12 of MATLAB Onramp.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!