How to import csv files in MATLAB?

24 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Cris LaPierre
Cris LaPierre il 16 Nov 2021
Use readtable, readmatrix, or readcell. My preference is readtable.
  6 Commenti
Abraham Caceres
Abraham Caceres il 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 il 18 Nov 2021
You need to implement some flow control based on the file name. See Ch 12 of MATLAB Onramp.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by