Hi all,
I intend to read multiple files in the for loop operation from the folder and subfolder, which got different file extensions. For instnace, the main folder (say Folder A) got .jpg fiels and subfolder A1 got .bmp files.
%% read files from folder A
for f = 1:1:1
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.jpg');
I=imread(fname);
G = imagesc(x,y,flipud(I));
G.AlphaData = 0.5;
hold on;
%%%%%%% read files from subfolder A1
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.jpg');
h=imread(fname);
H1 = imagesc(flipud(h));
H1.AlphaData = 0.5;
axis equal
hold on;
end

댓글 수: 1

As an aside, you should replace all of that complex IF/ELSEIF/ELSE, NUM2STR, and STRCAT with this:
fname = sprintf('B%05d.vc7',j)

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

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 1일

1 개 추천

You can use dir() and for-loop to iterate over files in a folder to process them. This link shows a general template: https://www.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html. For example,
%% read files from folder A
files = dir('*.jpg');
for i = 1:numel(files)
filepath = fullfile(files(i).folder, files(i).name);
img = imread(filepath);
% process the file
end
%% read files from subfolder A1
files = dir(fullfile('A1', '*.bmp'));
for i = 1:numel(files)
filepath = fullfile(files(i).folder, files(i).name);
img = imread(filepath);
% process the file
end

댓글 수: 11

Thanks, Ameer.
I have revised my parent code based on this.. Now, I can read files from folder and subfolder respectively..
Ameer Hamza
Ameer Hamza 2020년 12월 1일
I am glad to be of help!
I have used this with differen extension files, But I ended with some error.
The command loadvec() will work only if format is loadvec ('.vc7') or loadvec ('.im7'), hence, I replaced the img = loadvec(files(i).name); instead of img = imread(filepath);.
This works perfectly for the files reading from Folder A, However, it throws error while reading file from subfolder A1. I guess, its due to filepath is not mentioned.
%% read files from folder A
files = dir('*.im7');
for i = 1:numel(files)
filepath = fullfile(files(i).folder, files(i).name);
img = loadvec(files(i).name);
% process the file
end
%% read files from subfolder A1
files = dir(fullfile('A1', '*.vc7'));
for i = 1:numel(files)
filepath = fullfile(files(i).folder, files(i).name);
img1= loadvec(files(i).name);
% process the file
end
Ameer Hamza
Ameer Hamza 2020년 12월 1일
How is the function loadvec defined?
loadvec() is one of the functions in pivmat toolbox package. http://www.fast.u-psud.fr/pivmat/
It specially to read the files in .im7, .vc7 format.
Ameer Hamza
Ameer Hamza 2020년 12월 1일
Can you show the complete error message? It might be some ussue with loadvec() function.
Here, it is..
It reads the .im7 file without any issue. But not .vc7 file (i.e. file inside subfolder A1). Since, I am not using loadvec(filepath), hence, it's still looking to read the file from folder A rather than folder A1. This is the reason it throws 'No file match.' error message..
Somehow, if we provide the filepath correctly, I guess it will read .vc7 files.
Error using loadvec (line 238)
No file match.
Oh! I get it. You are only passing the filename to loadvec(). What happens if you pass the complete path? Does it gives error? For example, in your code, write
filepath = fullfile(files(i).folder, files(i).name);
img1= loadvec(filepath);
Hi,
Just noticed one thing, if my folder is located, let say in D drive , in this case my filepath = D:\New folder\A1\B00001.vc7. Here, it works perfectly for both folder A and subfolder A1.
But If I read the file from my server, in that case filepath = \\Data\data_\New folder\A1\B00001.vc7. Here it throws error. The sad part is I have to read file from my server.. (Lol!!!)
This is definitely some problem with loadvec(). As the error message indicate that error occurs on line 238, which is
if isempty(filename)
which means that filename somehow become empty. And if you look at lines before 238, you can see that the function is doing some processing on the variable 'filename'. You may add a breakpoint inside the loadvec() function and see what causes the filename to become empty.
While my file is loacted in my D drive, it becomes
loadvec ('D:\New folder\A1\B00001.vc7') and this is working perfectly..
However, if i read from my server, it will be
loadvec ('\\Data\data\New folder\A1\B00001.vc7'); Here comes the error..

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Search Path에 대해 자세히 알아보기

질문:

2020년 12월 1일

댓글:

2022년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by