function writefiles(dest_folder)
addpath(genpath(dest_folder)); %add folder to the path
filesandf=dir(dest_folder); %display contents of folder
filesinfolder= filesandf(~([filesandf.isdir])); %show only files of folder
fnames= {filesinfolder.name}; %show names of all in a cell
numoffiles=length(fnames); %number of files
k=1;
while(k<numoffiles)
filename=filesinfolder(k).name;
fid=fopen(filename); %open the file
tline = fgetl(fid); %read file line by line
while ischar(tline) %iterate through every line of file
if contains(lower(tline),'import from') %check for "import from" keyword
tests_after_import_from=regexp(tline,'\w*.art\w*', 'match'); %if present,check for any string containing .art in the same line
ind=ismember(tests_after_import_from, fnames); %check if the string containing .art is present in the dest_folder
if ind~=1
% here i want to write the filename and
% tests_after_import_from into columns of excel file 'A'
% and 'B' respectively
end
end
tline = fgetl(fid); %read next line
end
fclose(fid); %close the file
k=k+1; %next iteration
end
end
Hello all,
in this code i am getting two outputs one is tests_after_import_from and filename .now i want to write all outputs which these two variables are giving to excel file . where filename wil come in 'A' column and tests_after_import_from wil go in 'B' column after the if ind~=1 is satisfied.how can i do that..
please help...

댓글 수: 2

Stephen23
Stephen23 2021년 3월 21일
Why do you need to a data folder to the MATLAB path?
In general it is more efficient to keep folders of data files off the MATLAB search path.
Stephen is correct. Do not use addpath() or savepath() in your code.
Just use fullfile() to construct the full file name from the base file name and the folder:
baseFileName = filesinfolder(k).name;
fullFileName = fullfile(dest_folder, baseFileName)
fid = fopen(fullFileName, 'rt'); % Open the file as text.

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

 채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 19일
편집: ANKUR KUMAR 2021년 3월 21일

0 개 추천

for k=1:numoffiles
filename=filesinfolder(kkk).name;
fid=fopen(filename); %open the file
tline = fgetl(fid); %read file line by line
while ischar(tline) %iterate through every line of file
if contains(lower(tline),'import from') %check for "import from" keyword
tests_after_import_from=regexp(tline,'\w*.art\w*', 'match'); %if present,check for any string containing .art in the same line
ind=ismember(tests_after_import_from, fnames); %check if the string containing .art is present in the dest_folder
if ind~=1
output_table{kk,1}=filename;
output_table{kk,2}=tests_after_import_from;
end
end
tline = fgetl(fid); %read next line
end
fclose(fid); %close the file
end
xlswrite('output_file',[{'host','imported'};output_table])
Hope it helps.

댓글 수: 8

Rohit P
Rohit P 2021년 3월 21일
편집: Rohit P 2021년 3월 21일
Thank you so much for answering the question.
It is giving error of variable kk undefined.
And also i have to write all the outputs of variable filename under the column name 'Host' and tests_after_imported_from in 'imported' in an excel file.
Please help in that too
ANKUR KUMAR
ANKUR KUMAR 2021년 3월 21일
Updated the code. Make sure that last line of the code will work only if output_table has two columns.
Rohit P
Rohit P 2021년 3월 21일
Thank you for replying
Two columns are getting created.but only filename is coming in the first column tests_after_import_from is not getting printed in file.
Image Analyst
Image Analyst 2021년 3월 21일
You accepted the answer so we assume you got your last problem figured out on your own since you posted your last comment.
Rohit P
Rohit P 2021년 3월 22일
I accepted the answer because the filename is correctly getting written in the excel..but the variable tests_after_import_from is not getting printed in the imported column.that is the only issue now..please help
Image Analyst
Image Analyst 2021년 3월 22일
편집: Image Analyst 2021년 3월 24일
OK, so it's not working. I'll give @ANKUR KUMAR a chance to fix his answer first. If he doesn't, or if you still need help after that, unaccept the answer and someone may open the post and answer. Like we both said, the problem was that you never called any function to write your results to Excel, like xlswrite(), writecell(), or other such functions.
I think I'd like to add is that even though xlswrite() is deprecated and they recommend you don't use it anymore, its suggested replacement of writematrix() or writecell() are not entirely a good replacements (yet) because using them on an existing workbook will wipe out any formatting you have done on the file, like fonts, cell colors, underlining, etc. xlswrite() will maintain those.
ANKUR KUMAR
ANKUR KUMAR 2021년 3월 22일
@R V P Is that variable you are writing in second column a string or double?
If you attach your complete code, along with the sufficient varibales in a mat file, it would help us to help you quickly and efficiently.
Rohit P
Rohit P 2021년 3월 24일
편집: Image Analyst 2021년 3월 24일
The issue was from my side. I have corrected it now, and the code is running perfectly. Thank you for the solution.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 3월 19일

0 개 추천

Where are you calling xlswrite (deprecated) or writematrix() or writecell()?

댓글 수: 1

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 19일
writematrix has been introduced in 2019a version. If someone is using older version, xlswrite works well.

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

카테고리

제품

릴리스

R2018b

질문:

2021년 3월 19일

편집:

2021년 3월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by