How to import csv files in MATLAB?

22 views (last 30 days)
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
Abraham Caceres
Abraham Caceres on 18 Nov 2021
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)
Cris LaPierre
Cris LaPierre on 18 Nov 2021
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!