Processing of files from different subfolders and write new file to the same folder

4 views (last 30 days)
I was considering updating a current script to do some multi file processing, since I've got like 30 different folders containing different .txt files with different names.
Currently I have a script that imports and processes a text file, using the dlmread command (see code). It calculates a bunch of stuff like slope, regression and so on. On the basis of the calculation, it saves figures as .png and creates another .txt file. It saves them on the basis of the name of the imported file, so that the new file contains a prefix like 'rate' or 'slope'. The import code looks like this:
name='mydata.txt';
folder1 = 'folder1';
folder2 = 'folder2';
folder3 = 'folder3';
ff = fullfile( fn1 , fn2 , name );
impdata=dlmread(ff);
The thing is, that I have to call my .txt files from different subfolders and with different names. Furthermore, it does not save to the same directory. I would like the code to search for all .txt files and process them one by one. Therefore, I looked at the dir command, and a quick look at the rdir command available for download. But from what I see, these commands will not work with my current script, as I write a prefix to the variable 'name' each time I process a file. So how to solve this? I tried looking at the following:
[ignore, files] = system( 'dir /B /S *.txt' );
folders = textscan( files, '%s', 'delimiter', '\n' );
The idea was to create some what a matrix containing the folders and .txt files to process to be used in my current matlab script - without succes, though. I got stuck on the creation of the matrix, since I have to exclude the backslashes and put in each folder and filename into a column for itself. So, how do I get on from this, to use it in my current code? Or quite simply, is there another (better) way for doing this?

Answers (1)

Geoff Hayes
Geoff Hayes on 18 Nov 2014
Lasse - how does writing a prefix to each file cause a problem with using rdir?
Suppose I run the following command
myJpgFiles = rdir('/Users/geoff/Development/bitbucket_repos/matlab/image*/**/*.jpg');
which returns an array of structs, one for each jpeg that I have over several folders beneath the above directory. I can then iterate over each file and split it apart into its folder, filename, and extension pieces. Once done, then the filename can have a prefix added to it. Something like
for k=1:length(myJpgFiles)
% get the path and file name
pathAndFile = myJpgFiles(k).name;
% split the path and file name into parts
[folder,filename,ext] = fileparts(pathAndFile);
% add a prefix to your file name
filename = ['pre' filename ext];
% rebuild the path and file name
pathAndFile2 = fullfile(folder,filename);
end
Try the above and see what happens!

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!