Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: last file in loop ignored, why?
Replies: 1   Last Post: Jan 24, 2013 1:37 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Steven Lord

Posts: 17,945
Registered: 12/7/04
Re: last file in loop ignored, why?
Posted: Jan 24, 2013 1:37 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Michael " <m.blackett@noc.soton.ac.uk> 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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2016. All Rights Reserved.