필터 지우기
필터 지우기

How can Looping (open text files, processing and save as Excel file) for multi times ?

조회 수: 1 (최근 30일)
Hi , Please , i have multi text files , i need looping : open ,processing, saving the results as excel file . repeat this process multi times based on number of text files , i have the code for open , processing , and save, process and save as excel for one file each time! so i should change text file name and excel file name munally each time !.
% How can repeat this process for multi text files ?
fid = fopen('stats1.txt','r');
strData = textscan(fid,'%s','Delimiter','\r\n');
strData = strData{1};
fclose(fid);
% Eliminate empty line(s) and the line(s) which contains '--'
idx1 = cellfun(@isempty,strData);
idx2 = contains(strData,'--');
strData = strData(~idx1 & ~idx2);
% Insert space before '#'
strData = replace(strData,'#',' #');
% Replace 3 or more consecutive spaces to 2 consecutive spaces
strData = regexprep(strData,'\s{3,}',' ');
% Prepare the output cell array
outData = cell(size(strData,1),6);
% Split each line with 2 consecutive spaces
for kk = 1:size(strData,1)
c = strsplit(strData{kk},' ');
outData(kk,[1:numel(c)-1, end]) = c;
end
% Save as Excel file
writecell(outData,'result1.xlsx');

채택된 답변

Furat Alobaidy
Furat Alobaidy 2019년 11월 28일
Still i have problem with this insturaction : how can avoid that ?Please
%% Save result file
xlswrite(result,strcat(outDirectory,text_files(i).name));
Error using xlswrite:
File name must be a string scalar or character vector.
Error in:
xlswrite(outData,strcat(outDirectory,text_files(i).name));
  댓글 수: 1
Image Analyst
Image Analyst 2019년 11월 28일
Try it this way:
%% Save result file
fullFileName = fullfile(outDirectory, text_files(i).name)
xlswrite(result, outDirectory,text_files(i).name);

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 28일
text_files=dir('C:\complete path....\folder_name\*.txt');
outDirectory='C:\complete path....\folder_name\';
for i=1:length(text_files)
text_file=strcat('C:\complete path....\folder_name\',text_files(i).name);
text_data=(textscan(text_file));
%% Do operation say result
%% Save result file
xlswrite(result,strcat(outDirectory,text_files(i).name));
end

Furat Alobaidy
Furat Alobaidy 2019년 11월 28일
편집: Furat Alobaidy 2019년 11월 28일
thanks for your help , but its gave me error :
Error using xlswrite (line 170)
File name must be a string scalar or character vector.
Error in test2 (line 26)
xlswrite(outData(i),strcat(outDirectory,text_files(i).name));
outDirectory='C:\Users\...\1-FromStatstoExcel\';
txtpattern = fullfile(outDirectory, '*.txt');
text_files = dir(txtpattern);
for i=1:length(text_files)
fid=strcat('C:\Users\...\1-FromStatstoExcel\',text_files(i).name);
strData=textscan(fid,'%s','Delimiter','\r\n');
strData = strData{1};
% Eliminate empty line(s) and the line(s) which contains '--'
idx1 = cellfun(@isempty,strData);
idx2 = contains(strData,'--');
strData = strData(~idx1 & ~idx2);
% Insert space before '#'
strData = replace(strData,'#',' #');
% Replace 3 or more consecutive spaces to 2 consecutive spaces
strData = regexprep(strData,'\s{3,}',' ');
% Prepare the output cell array
outData = cell(size(strData,1),6);
% Split each line with 2 consecutive spaces
for kk = 1:size(strData,1)
c = strsplit(strData{kk},' ');
outData(kk,[1:numel(c)-1, end]) = c;
end
xlswrite(outData(i),strcat(outDirectory,text_files(i).name));
end

Image Analyst
Image Analyst 2019년 11월 28일
If you still have trouble with your adaptation of the FAQ code, include it in your reply.

Furat Alobaidy
Furat Alobaidy 2019년 11월 28일
Thanks for all about this helping .
its running now .

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by