How to save input main folder name as the output filename
조회 수: 2 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
답변 (1개)
Geoff Hayes
2016년 1월 29일
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
Geoff Hayes
2016년 1월 31일
Kanakaiah - in your above code, where do you specify the main folder, the sub-folder, and the sub-sub-folders?
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!