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

채택된 답변

dpb
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
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...
Lina Koronfel
Lina Koronfel 2020년 2월 21일
Thank you alot for the help! I implemented your answers and now I used the code multiple times and it is working fine. This is the final code I'm using in case it would benefit others. Cheers.
ImpFrames=250; %Truncate trial to only important frames
StartInt=800;
tifFiles=dir(fullfile(myFolder,'*.tif'));
tifFiles=natsortfiles({tifFiles.name});
for i=200:StartInt:8000;%length(tifFiles)----All frames of trials to be analyzed;
for j=i:i+ImpFrames; %Selected frames/trial to be analyzed
fullFileName=fullfile(myFolder, tifFiles{j});
fprintf('Now reading %s\n', fullFileName);
imageArray_uncrop=imread(fullFileName);
%whatever...
end
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by