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

1 view (last 30 days)
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.
Cheers.

Accepted Answer

Mathieu NOE
Mathieu NOE on 15 Sep 2021
hello
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 :
clc
clearvars
%% read one file (example 1)
filename = 'beta_1_kappa_05_cbeta1_1.txt';
[data1,data2] = myreadfunction(filename);
figure(1)
plot(data1)
hold on
plot(data2)
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({fileNames.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
m = length (fileNames_sorted);
%% main loop for data extraction and concatenation
outdata = [];
figure(2);
for k = 1:m
[data1,data2] = myreadfunction(fullfile(fileDir, fileNames_sorted{k})) ;
plot(data1)
hold on
plot(data2)
% your code here...
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
end

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!