How to load selected files, in a sequence, and implement them in a for loop?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
dpb
on 30 Dec 2019
Edited: dpb
on 30 Dec 2019
"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 Comments
dpb
on 2 Jan 2020
Edited: dpb
on 4 Jan 2020
"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...
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!