How to make calculations between two specific files via loop?

조회 수: 1 (최근 30일)
Ivan Mich
Ivan Mich 2021년 3월 12일
댓글: Ivan Mich 2021년 3월 12일
Hello,
I have a question about a code. I have in a directory some .xlsx files and some .txt files. I want to make calculations between xlsx files and .txt files. The problem is that I want to make these calculations via a loop depending on the name characters.
I want my code to
1)Read xlsx files and txt files
2)Make calculations between files that have only the same 4 characters of their name (for example one file has name: 'AGR3upto10km.xlsx' and the one other 'AGR3upto10kmmeano10km.txt' - I mean "AGR3")
I want to do operations only between files that have the first 4 characters in their names same. After that I want to find the other two characters with the same 4 characters in their name and make operations etc..
I use importdata for .txt and xlsread for .xlsx files
Could you please help me?
  댓글 수: 2
Jan
Jan 2021년 3월 12일
"After that I want to find the other two characters with the same 4 characters in their name and make operations etc.."
I do not understand this sentence.
Ivan Mich
Ivan Mich 2021년 3월 12일
Look, I have as I said multiple .txt and .xlsx files. I want to take 2 files (one .txt and one .xlsx) that have same 4 charactres of their name. (Lets say 'AGR3upto10km.xlsx' and the one other 'AGR3upto10kmmeano10km.txt' ) . after that I want to take files 'GRA3upto10km.xlsx' and the one other 'GRA3upto10kmmeano10km.txt' and make calculations. etc

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

채택된 답변

Jan
Jan 2021년 3월 12일
Folder = 'C:\Your\Folder';
XLSList = dir(fullfile(Folder, '*.xlsx'));
TXTList = dir(fullfile(Folder, '*.txt'));
TXTName = {TXTList.name};
for k = 1:numel(XLSList)
name = XLSList(k).name;
match = strncmp(TXTName, name(1:4), 4);
if nnz(match) ~= 1
fprintf('No unique match for %s\n', name);
continue;
end
TXTFile = fullfile(Folder, TXTName{natch});
XLSFile = fullfile(Folder, name);
... Now do what you want with them
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by