MATLAB Answers

Reading multiple non-sequential *.txt files containing both text and numbers

5 views (last 30 days)
Oguzhan M
Oguzhan M on 13 Sep 2021
Commented: Mathieu NOE on 20 Sep 2021 at 6:42
Hi everyone,
I'm a bit struggling with importing large number of text files into Matlab. I think the bit I'm having difficulty is "delimeter" and number of columns in each file where it starts with 2 column then 3 on the second line then 2 in the rest. I've tried textscan, load and importdata so far but had no luck.
On a seperate note, I do not need the text, I just need the numbers to be imported.
To give you a deeper insight, I attach the some of the files. These are the first batch. The second batch of files comes with a name:
  • beta_1_kappa_05_cbeta2_1.txt
  • beta_1_kappa_05_cbeta2_5.txt
  • beta_1_kappa_05_cbeta2_10.txt
  • beta_1_kappa_05_cbeta2_20.txt
  • beta_1_kappa_05_cbeta2_30.txt
However, the third batch does not include "..cbeta3_30.txt", therefore this batch looks something like this:
  • beta_1_kappa_05_cbeta3_1.txt
  • beta_1_kappa_05_cbeta3_5.txt
  • beta_1_kappa_05_cbeta3_10.txt
  • beta_1_kappa_05_cbeta3_20.txt
Then, the following batches loop for "kappa" for kappa=10, 15, 20 as in kappa_10, kappa_15 and kappa_20. Finally, everything will loop for beta_2 and beta_3.
I hope what I'm trying to achive is clear, and I can get some help.

Accepted Answer

Mathieu NOE
Mathieu NOE on 15 Sep 2021
this is an example that shows how I could read one then multiple text files (and applying a natural name sorting)
you can expand on that
I noticed the data are spread in two sections in your files but the code shows how to retrieve both data sets;
you can build on that , I don't know how you want to further organize the data as you process all the text files;
I attached the two functions for convenience
my code is bellow :
%% read one file (example 1)
filename = 'beta_1_kappa_05_cbeta1_1.txt';
[data1,data2] = myreadfunction(filename);
hold on
hold off
%% read multiple files (example 2)
fileDir = pwd; % currrent directory
fileNames = dir(fullfile(fileDir,'*.txt')); % get list of files in directory
fileNames_sorted = natsortfiles({}); % sort file names into order (
m = length (fileNames_sorted);
%% main loop for data extraction and concatenation
outdata = [];
for k = 1:m
[data1,data2] = myreadfunction(fullfile(fileDir, fileNames_sorted{k})) ;
hold on
% your code here...
function [data1,data2] = myreadfunction(filename)
a = readlines(filename);
% find / extract the two sets of data - separated by text
start_indexes = find(contains(a,'(('));
stop_indexes = find(strcmp(a,')'));
data1 = str2num(char(a(start_indexes(1)+1:stop_indexes(1)-1)));% first (upper) segment of data
data2 = str2num(char(a(start_indexes(2)+1:stop_indexes(2)-1)));% second (lower) segment of data

More Answers (0)




Community Treasure Hunt

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

Start Hunting!