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

2 views (last 30 days)
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 :)

Answers (1)

Cris LaPierre
Cris LaPierre on 21 Mar 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.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!