Borrar filtros
Borrar filtros

How do I pull data from multiple excel spreadsheets located in the same folder?

45 visualizaciones (últimos 30 días)
Abigail
Abigail el 23 de Jul. de 2024 a las 14:52
Comentada: dpb el 24 de Jul. de 2024 a las 18:08
Hello!
I'm trying to record a specific data point from multiple excel files that are located in the same folder and dump them into their own table.
To elaborate, I have thousands of excel files all under the path 'C:\Users\Me\DataFiles'. I want to open each file, take the data from cell M18, and put it into its own table with all the data points.
I currently have a line using the readmatrix function and I'm able to read just the first file in the folder, but nothing else. I already have a table populated with serial number information, so I get an error that the table variables must have the same number of rows.
Any help would be greatly appreciated, thank you!
  3 comentarios
Abigail
Abigail el 23 de Jul. de 2024 a las 15:47
Hi Cris! It's difficult for me to share the code and data because it's on a different server.
But I want to take all of the M18 data points and put it into one singular excel file so there will be x amount of rows for the amount of files that it takes data from.

Iniciar sesión para comentar.

Respuestas (2)

Cris LaPierre
Cris LaPierre el 23 de Jul. de 2024 a las 16:51
I would use a fileDatastore to load all the data into a single variable. You can see an example of how to use one to do this in this video from the Data Processing with MATLAB specialization on Coursera.
Here is the final code from that example. You can modify this to work for your data.
flightsDataStore = fileDatastore("flights*.csv","ReadFcn",@importFlightsData,"UniformRead",true);
flightsAll = readall(flightsDataStore)
Once complete, all the data from all files with names matching the pattern "flights*.csv" are loaded into the variable flightsAll.
  4 comentarios
Cris LaPierre
Cris LaPierre el 24 de Jul. de 2024 a las 13:20
What does your code look like? Are you getting any sort of error message?
Perhaps try using fullfile to build your paths?
dpb
dpb el 24 de Jul. de 2024 a las 18:08
"...try using fullfile to build your paths?"
Indeeed. If you will use fullfile at the command line, it will do auto-complete of the file path, easily verifying that you're going where you think you are first and checking the wildcard will match, later.

Iniciar sesión para comentar.


dpb
dpb el 23 de Jul. de 2024 a las 16:08
"But I want to take all of the M18 data points and put it into one singular excel file so there will be x amount of rows for the amount of files that it takes data from."
Basically a trivial exercise albeit it may take a while...
SHT='YourSheet'; % which sheet to read (name or numeric)
CELL='M18'; % the cell address of interest
baseFolder='Yourfolderpath'; % where they're located
baseFileName='YourFileName*.xlsx'; % a wildcard filename that will match those of interest
FQFN=fullfile(basefolder,baseFileName); % build the fully-qualified file name
d=dirFQFN); % return the dir struct of those that match
N=numel(d); % how many files are there???
data=nan(N,1); % preallocate a column vector of that many
for i=1:
data(i)=readmatrix(FQFN,'Sheet',SHT,'Range',CELL); % populate the array
end
data=array2table(data,'VariableNames',CELL); % convert to a table
Best practice would be to encapsulate the above in a function that can be called with the appropriate input data and return the result.
  1 comentario
Walter Roberson
Walter Roberson el 23 de Jul. de 2024 a las 16:51
for i=1:
data(i)=readmatrix(FQFN,'Sheet',SHT,'Range',CELL); % populate the array
? You are missing the upper bound of the for loop ?
The body of the loop does not appear to vary with the loop ?

Iniciar sesión para comentar.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by