Combining many .csv into a single table

9 visualizzazioni (ultimi 30 giorni)
Garth Mortensen
Garth Mortensen il 17 Dic 2018
Spostato: Cris LaPierre il 27 Ago 2025
Hello,
I am a trying to plot the content of many csv files, using a loop. So far, I've adjusted this solution to fit my purpose. The next step is to merge the content of these output files into a single table, so that I can plot them. But how to merge/combine?
Walkthrough
1) All csv are in the same directory.
2018-12-17 13_34_37-good.png
2) All csv are identically formatted.
2018-12-17 13_33_03-2018.12.17.13.10.47_output.csv - Excel.png
3) My existing code:
dinfo = dir('C:\output\*.csv');
for K = 1:length(dinfo)
thisfilename = dinfo(K).name; %filename
thisdata = readtable(thisfilename); %load file
% somehow plot the results, perhaps by combining thisdata each K step
end
Current Results
I can load each file into Matlab.
2018-12-17 13_46_12-MATLAB R2018b - student use.png
Next Goal
How to achieve a single table combined of all csv column 2?
2018-12-17 13_51_41-2018.12.17.13.10.47_output.csv - Excel.png
End goal will be to plot the results, such as:
CreateLinePlotFromMatrixExample_01.png
Thank you for any direction/advice!
Garth
  1 Commento
Gareth
Gareth il 17 Dic 2018
Hi Garth,
There is a simple solution for what you are trying to do:
ds = datastore(pwd);
mydata = ds.readall;
This creates a datastore, that can be configured to look for specific file types, e.g. *.csv in a folder. It also has some properties where you can specify how each file should be read. Very similar to the readtable comand that you are already using.
The readall, reads every file and puts it in 1 table.
Hope this helps.

Accedi per commentare.

Risposte (1)

Cris LaPierre
Cris LaPierre il 18 Dic 2018
I would think using the function join should do it. You can combine tables using the rownames, which should be the same in all your files.
  4 Commenti
Emilia
Emilia il 27 Ago 2025
Spostato: Cris LaPierre il 27 Ago 2025
Hi,
I have in my directiry 173 csv files like this with different filenames (different FTIR spectra). How can I combine them in a single table with a single x= wavelenght (cm-1) and 173 column corresponding to the 173 absorbance spectra titled with name of each single file?
Thanks
Emilia
Cris LaPierre
Cris LaPierre il 27 Ago 2025
Spostato: Cris LaPierre il 27 Ago 2025
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 matching the pattern "flights*.csv" are loaded into the variable flightsAll.

Accedi per commentare.

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by