For loop fails after a good start
1 view (last 30 days)
Show older comments
Hi, I have inherited a script to read in a series of data files. It seems to work perfectly well up to 16 files and then fails on the 17th.
I have tried a variety of input combinations always resulting in the same outcome.
Any suggestions would be appreciated.
Script:
clear
expno=(201:220);
for i=1:length(expno)
currexpno=expno(i)+0;
disp(['expno ' num2str(currexpno)])
[rsp(i,:),isp,ppm]=MolpageNMRreadFree('/Users/dthomas21/Tesla/data/dst/nmr/',currexpno,'horse-combo',1);
end
Here is the output from the script.
expno 201
...
expno 215
expno 216
expno 217
sorry mate no 1r file
sorry mate no 1i file
expno 218
**** file id = -1, prob wrong directory ****
Error using fread
Invalid file identifier. Use
fopen to generate a valid file
identifier.
Error in MolpageNMRreadFree (line 27)
Here is the MolpageNMRreadFree section
%script for finding the byte order
procpars=fullfile(disk,experiname,num2str(expnum),'pdata',num2str(procno),'procs');
fileid=fopen(procpars);
if fileid == -1
disp('**** file id = -1, prob wrong directory ****')
end
%this is line 27
theprocs=fread(fileid);
theprocs=char(theprocs)';
%find the index of the string "BYORDTP" which is the byte order
%(big or little endian) of the processed data
k1=findstr(theprocs,'BYTORDP');
%%now get a string that defo has the byte number in it
strbiteno=theprocs(k1:k1+230);
%now find where the equals sign is
strequalsign=findstr(strbiteno,'=');
startofbiteorderline=strequalsign(1)+1;
I'm using R2011b 64 bit on a Mac in case that makes any difference.
Thanks,
Don
1 Comment
Naz
on 22 Nov 2011
Your code is useless to me because I can not load the file you have. However, if you showed the error report, then it could be possible to guess the reason of failure.
Answers (1)
Walter Roberson
on 22 Nov 2011
I suggest you replace the lines
fileid=fopen(procpars);
if fileid == -1
disp('**** file id = -1, prob wrong directory ****')
end
by
[fileid, message]=fopen(procpars);
if fileid == -1
warning(sprintf('open failed for %s, skipping file: ', procpars, message))
thisrsp = []; %change according to formal names of output parameters
thisisp = [];
thisppm = [];
return
end
And where you have
[rsp(i,:),isp,ppm]=MolpageNMRreadFree('/Users/dthomas21/Tesla/data/dst/nmr/',currexpno,'horse-combo',1);
change that to
[thisrsp, isp, ppm] = MolpageNMRreadFree('/Users/dthomas21/Tesla/data/dst/nmr/',currexpno,'horse-combo',1);
if ~isempty(thisrsp) %empty if failed to read this file
rsp(i,:) = thisrsp;
else
%I might suggest filling with NaN if a file was unreadable, but if it is the first file that is unreadable we might not know how many columns to expect
end
If you decide that you want a unreadable file something worth quitting for, you can do that cleanly in the "else" branch; the MolpageNMRreadFree routine should not really be making that decision.
2 Comments
Walter Roberson
on 29 Nov 2011
Your script should be closing fileid after you are finished reading from that file, which is probably right after the fread() line.
Failing after 16 files would be unusual, but failing after a total of 255 open simultaneously (including standard input and output) would be common. Some systems, it is 127 total. Anyhow.
See Also
Categories
Find more on Whos 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!