How do you open multiple .txt files with names count_#.txt where # goes from 1-4?

2 visualizaciones (últimos 30 días)
Hi, I am trying to open four data files - count_1.txt, count_2.txt, count_3.txt, and count_4.txt. Each contains four columns of data. I can open one of them with the following two lines:
filename = 'coords_1.txt';
nodedata = readmatrix('filename');
But I wanted a "for" loop which reads the data from each file into one mega matrix stacked ontop of each other. So if each file has 20 rows of 4 columns of data, the mega matrix I create will have 80 rows of the 4 columns of data. TYIA :)

Respuestas (1)

Cris LaPierre
Cris LaPierre el 21 de Mzo. de 2023
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.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by