How to save input main folder name as the output filename

1 view (last 30 days)
Hi,
I have a main folder, and inside many sub folders. Each sub-folder contains text files. I want to read the text file, and save required data. Now, I merge all subfolders in to one main folder, and running my program, and save data. I am manually modifying the output file name each time when I run different main foldres (each main folder corresponding to different doposition tools. But, now I wish to just give the main folder location, and want to save put file name as the input main folder name. My main folder name(s) are like tool1, tool2 etc. My out files are "score_tool1.csv", "parameter_tool1.csv"
my code is below:
clc;
clear all;
clc
tic
FileList=dir('D:\Mekala_Backupdata\Matlab2010\Filesfolder\PartofTextFilesData/');
j=1;
for i=3:1:(size(FileList)) %%read all files from folder of specified dir
FileName{j}=FileList(i).name;
j=j+1;
end
for j=1:size(FileName,2)
fid=fopen(['D:\Mekala_Backupdata\Matlab2010\Filesfolder\PartofTextFilesData/',FileName{j}],'r'); %%opening each files and read each line
allText = textscan(fid,'%s','delimiter','\n');
numberOfLines = length(allText{1});
allText=allText{:};
for k=1:size(allText,1)
idx_RainFallID=find(~cellfun(@isempty,regexpi(allText,'RainFallID')));
idx_LMDName=find(~cellfun(@isempty,regexpi(allText,'LMD Name')));
idx_10Under1=find(~cellfun(@isempty,regexpi(allText,'10 Under Pipe 1.Response Value')));
idx_RainFallIDtemp=allText(idx_RainFallID);
idx_RainFallIDtemp2=regexp(idx_RainFallIDtemp,' +','split');
b(j,1)=str2double(idx_RainFallIDtemp2{1}{1,3});
Variable{1,1}=char(idx_RainFallIDtemp2{1}{1,1});
end
fclose(fid)
end
xlswrite('score_tool1.csv',b)
xlswrite('parameter_tool1.csv',Variable)

Answers (1)

Geoff Hayes
Geoff Hayes on 29 Jan 2016
Kanakaiah - if your (sub?) folder is named as
folder = 'tool1';
and you want to incorporate into the filename, then you could try
scoreFilename = ['score_' folder '.csv'];
paramFilename = ['parameter_' folder '.csv'];
xlswrite(scoreFilename,b)
xlswrite(paramFilename,Variable)
  2 Comments
Kanakaiah Jakkula
Kanakaiah Jakkula on 30 Jan 2016
I might have confused you. It is three level, (1) main folder,(2) Sub-folder,(3) sub-sub-folders. It means, I have a main(root folder), and in this folder, there are sub-folders named with different tool ID's, and inside these sub-folders, there are several folders (sub-sub-folder(s)) which contains textfiles (actually I want to read). My out put files names should contain sub-folder name(not the sub-sub-folder).
Geoff Hayes
Geoff Hayes on 31 Jan 2016
Kanakaiah - in your above code, where do you specify the main folder, the sub-folder, and the sub-sub-folders?

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!