How to load selected files, in a sequence, and implement them in a for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I'm writing a code that process multiple images in a sequential order from a folder (basically frames of some video). However, my problem is that I want to collect information and process only specific frames that repeat after a constant frame number ( I want to process 400 frames, every 800 frames, and keep doing this for the length of the files wich is X0000 frames). I don't know which part of the code I should write that command though. Below is the first part of the code until loading the files. VIPFrames is my idea for which frames I want to call. Thank you!
myFolder= cd;
if exist(myFolder, 'dir')~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
tifFilesNotOrg=dir(fullfile(myFolder,'*.tif'));
tifFiles=natsortfiles({tifFilesNotOrg.name});
for F=1:length(tifFiles);
VIPFrames=200+(800.*F-800):600+(800.*F-800); %Limited Frames-stimulation-how to place it?
fullFileName=fullfile(myFolder,tifFiles{F});
fprintf('Now reading %s\n', fullFileName);
imageArray=imread(fullFileName,VIPFrames);
end
댓글 수: 0
채택된 답변
dpb
2019년 12월 30일
편집: dpb
2019년 12월 30일
"I want to process 400 frames, every 800 frames, and keep doing this for the length of the files..."
...
nProc=400;
nSkip=800;
tifFiles=dir(fullfile(myFolder,'*.tif'));
tifFiles=natsortfiles({tifFiles.name});
for i=1:nSkip:numel(tifFiles)
for j=i:i+nSkip-1
fullFileName=fullfile(tiffFiles(j).Folder,tifFiles(j).Name);
fprintf('Now reading %s\n', fullFileName);
imageArray=imread(fullFileName);
% whatever else here
end
end
댓글 수: 4
dpb
2020년 1월 2일
편집: dpb
2020년 1월 4일
"Unless dpb offers a good reason for sorting the dir output structure, ..."
Nothing more than consistency in way I tend to write code can I offer, no.
I just didn't think about the fact had turned the struct into a cell array as I was writing the response...I used my natural reference to the dir() struct by pure rote.
I am big on keeping the same style everywhere, however, so if it were my code needing to do this, I'd guess I'd sort the dir() struct about 99.73% of the times it came up (which would be almost never in the example as unless the files came from an outside source, I'd have named them such as they would sort alphanumerically and thereby already sorted as returned by dir). Unless Stephen has a good reason to NOT sort the dir() struct, ... <VBG>
However, it's your code, use the way that seems most natural to you. I tend to avoid cell arrays when I can; I'm always tending to forget the curlies and so make more coding errors to have to fix when do...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!