I'm trying to use csvread within a functino but am having issues with it finding the file I want to read in from. The error message returned is
??? Error using ==> csvread at 38
File not found.
'exist' returns 2, and when I run csvread on its own it finds the file ok. Needless to say, I'm a little confused! I've read similar problems which suggest these kind of errors may be due to corrupt m-files - if this is the case, how can I fix this? I'm not sure, particularly given that csvread works on its own...
Any suggestions?
Thanks
"Stephen " <ste...@reynolds.ph.man.ac.uk> wrote in message
news:i5jcu6$224$1...@fred.mathworks.com...
It would likely be easier to determine what's going on if you showed the
group a small section of your code.
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
Wait, what? The function exist returns 2 if the file is a MATLAB function, according to the documentation. And csvread reads, well, .csv files. Perhaps there is more to the story (which a small bit of code might help show us)?
Hi, thanks for your responses so far. The code is a loop over integer values, and the filename changes depending on the value of the integer. I use csvread to copy in the data from each file in succession.
My code is as follows - it's not massively clean, my apologies!
for n=1:m
eval(['time_value(' num2str(n) ') = (' num2str(n) '-1)*300 ;']);
eval(['if (time_value(' num2str(n) ') == 0) w = 0; X = csvread(''AG-t_' num2str(w) '.000E+0sec.dat'', 0, 0); end;']);
eval(['if ((time_value(' num2str(n) ') > 0) && (time_value(' num2str(n) ') < 1000)) w = time_value(' num2str(n) ')/100; X = csvread(''AG-t_' num2str(w) '.000E2sec.dat,'', 0, 0);end;']);
% eval(['csvread(''AG-t_' num2str(w) '.000E+2sec.dat'',0)'])
eval(['if ((time_value(' num2str(n) ') > 999) && (time_value(' num2str(n) ') < 10000) w = time_value(' num2str(n) ')/1000; X = csvread(''AG-t_' num2str(w) '00E+3sec.dat'', 0, 0); end;']);
end
it manages to read the first file, which is in the same location as the other files, but then fails to find the second one...
Maybe I'm going about this the wrong way? I'm not really sure how to set up a convenient loop which reads in the file names I require. The filenames are
AG-t_3.000E+2sec.dat etc, where the number increases by 300 between successive file names.
Thanks again for your responses - if this is nonsense let me know!
Why are you using all these evals? Very unnecessary, and probably the source of the problem (I'm guessing the file name being passed to csvread is probably not what you think it is). The following should be identical to your code, except I added a check for file existence:
for n = 1:m
time_value(n) = (n - 1)*300;
if time_value(n) == 0
w = 0;
elseif time_value(n) > 0 && time_value(n) < 1000
w = time_value(n)/100;
elseif time_value(n) >= 1000 && time_value(n) < 10000
w = time_value(n)/1000;
end
file = sprintf('AG-t_%d.000E+0sec.dat', w);
if exist(file, 'file')
X = csvread(file);
else
error('Could not find file %s', file);
end
end