How to make script look for specific files for processing?

2 views (last 30 days)
Hi all,
I was wondering if there was a way for my script to look for specific files to import (jpg files) so it can crop multiple images selected.
%Insert file names (without extensions) for processing
for LFAfile={'0W_1pc','0W_2pc','0W_3pc','0NoC_1pc','0NoC_2pc','0NoC_3pc'};
%Add the file extension and look for the file
if(exist(strcat(LFAfile{1}, '.jpg'),'file'))
LFA = imread(strcat(LFAfile{1}, '.jpg'));
%Uncropped set to 0 means that the image must still be manually
%cropped
if(uncropped==0)
%Image will appear in upper panel.
%Manually crop by drawing a box around the test and control lines
%Make sure the control line is in the left half of the crop window
%and the test line is in the right half of the crop window
subplot(3,1,1);
LFAGray = imcrop(rgb2gray(LFA), [46.5 3.5 219 31]);
close all;
else
LFAGray = rgb2gray(LFA);
end

Accepted Answer

dpb
dpb on 2 Jul 2014
Sure, if you can develop the pattern, you can generalize it...
for LFAfile={'0W_1pc','0W_2pc','0W_3pc','0NoC_1pc','0NoC_2pc','0NoC_3pc'};
if(exist(strcat(LFAfile{1}, '.jpg'),'file'))
It depends on whether there are other files of similar naming that are not wanted or not as to how much effort you need. If it's a subdirectory that's full of .jpg files that need be done, then the simplest is just
d=dir('*.jpg');
for i=1:length(d)
LFA = imread(d(i).name);
...
which eliminates the need to test for existance, etc., because dir won't include it if it doesn't.
Now the next most restrictive case in your example of names is that they all begin w/ 'O', so add that to the wildcard string--
d=dir('O*.jpg');
Next would require making up the two cases of 'OW*' and 'ON*' or perhaps it's the pc at the end of the name that is unique; no way for us to know that. There's also the alternative of getting the full array of .jpg files and then excluding ones not wanted therefrom. As many alternatives as you can dream up, depending on what the actual rules are for who is/isn't to be in the process list.
  3 Comments
dpb
dpb on 2 Jul 2014
Of course you could, but uigetfile is probably more appropriate since it has multi-select capability (that otomh I don't think uiopen has). You can (and will :) ), of course, check the doc's for the details...

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings 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!