How to import csv files in MATLAB?

26 visualizaciones (últimos 30 días)
Abraham Caceres
Abraham Caceres el 16 de Nov. de 2021
Comentada: Cris LaPierre el 18 de Nov. de 2021
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

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by