Read files from folder and subfolder
61 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2022 年 12 月 14 日
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
2020 年 12 月 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 件のコメント
Ameer Hamza
2020 年 12 月 1 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!