1

Could you please help me to read the data from a table.txt in a series of subfolders from a directory? In all the subfolders, the output to read has the same name, 'table.txt'. I want to process the data and save the output in the same folder.

I can process it using the following code.

   a = readmatrix('table.txt');
   a4 = a(:,4);
   a4 = a4 - mean(a4);
   N = 2^(nextpow2(length(a4)));
   freq = (abs(fftshift(fft(a4,N)))); 

   t=[0:1e-12:20e-9].';
   ts=t(2)-t(1);
   F = ((-N/2:N/2-1)/N)*(1/ts);

   fmr=[(F(N/2+1:end)/1e9)' freq(N/2+1:end)];
   writematrix(fmr, 'fmr.csv');
   cd folder

But how to perform the same action on all the subfolders?
Could somebody please help me out?

Sateesh
  • 25
  • 4

1 Answers1

1

You can use the "find files in subfolders" behaviour of dir. Something like this:

allTables = dir('**/table.txt');
for ii = 1:numel(allTables)
    thisFolder = allTables(ii).folder;
    inFile = fullfile(thisFolder, allTables(ii).name);
    a = readmatrix(inFile);    
    % do stuff ...
    fmr = ...
    outFile = fullfile(thisFolder, 'fmr.csv');
    writematrix(fmr, outFile);
end
Edric
  • 23,676
  • 2
  • 38
  • 40
  • Dear Edric, could you please help me to read the files in ascending order based on the maximum value of the file from the subfolders? I have table.txt files in the subfolders of a directory. First, I want to find the maximum value of a particular column of the table.txt file. Then, I want to read these files in an ascending order based on these values. I can read the files with your suggestion. But unable to sort the files in the way I want to process the data further. – Sateesh Nov 19 '22 at 10:20
  • You might wish to post this as a separate question. Briefly though, here's how I would approach things. 1. Read all the files in whatever order, store results in a `cell` array; while you're doing this, store the max value in a vector of max values. 2. Use the 2-output form of `sort`, i.e. `[sortedMax, order] = sort(maxValues)`. 3. Re-order the cell array using the `order` vector. – Edric Nov 21 '22 at 06:39