Date: Jan 24, 2013 1:37 PM
Author: Steven Lord
Subject: Re: last file in loop ignored, why?

"Michael " <> wrote in message
> Hello,
> Matlab beginner here,
> I have wrote a loop to import a number of data files as structures so as
> to perform a number of processes on each of the files.
> When I import and perform actions on the set of data files the last file
> is not included in the process. I don't know why...
> For example, here I am working with CTD data from an oceanographic cruise.
> I want to extract the temperature value if the depth it was recorded at
> was a specific depth. i.e. here I want the 2m depth temperature for all
> CTD casts.
> dataFolder='Folder_of_files'; fileExt=fullfile(dataFolder, '*.txt');
> txtFiles=dir(fileExt);
> for i=1:length(txtFiles); clear rawData
> rawData=importdata(txtFiles(i).name);
> if rawData(1,1)==2 % if the depth is == to 2m
> temp_2m(i)=rawData(1,4); % then extract the temperature value
> end end
> The problem I have is that despite the fact that txtFiles is composed of
> the total number of files (130) 'temp_2m' is only composed of 129, the
> 130th files is stored in rawData...

Well, for the last file is the first element EXACTLY, down to the last bit,
equal to 2? Even a minor difference would cause your IF to fail. Display the
difference between rawData(1, 1) and 2 for each case that did not satisfy
the IF condition and see if the data file is for a radically different depth
or 2.000000001 meters.

You should probably also preallocate temp_2m to be a NaN vector of the
appropriate size; that way you can easily detect if any of the previous
files also failed your exact equality test but were automatically filled
with a 0 by assignment to a later element of temp_2m.

If you find that the depth is VERY close to, but not exactly equal to 2, and
you want to still process those close-enough data files, compare with a
tolerance instead of using ==.

Steve Lord
To contact Technical Support use the Contact Us link on