Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with .csvread

8 views
Skip to first unread message

Stephen

unread,
Aug 31, 2010, 1:08:22 PM8/31/10
to
Hi,

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

Steven_Lord

unread,
Aug 31, 2010, 1:13:33 PM8/31/10
to

"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

Image Analyst

unread,
Aug 31, 2010, 1:15:09 PM8/31/10
to
Have you tried passing in the FULL path (folder + base file name)? Maybe you're just passing in the base filename and the file is somewhere else. Have you called exist() immediately before calling csvread?

Andy

unread,
Aug 31, 2010, 2:37:06 PM8/31/10
to
"Stephen " <ste...@reynolds.ph.man.ac.uk> wrote in message <i5jcu6$224$1...@fred.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)?

Stephen

unread,
Sep 2, 2010, 1:35:26 PM9/2/10
to
"Stephen " <ste...@reynolds.ph.man.ac.uk> wrote in message

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!

Kelly Kearney

unread,
Sep 2, 2010, 5:23:37 PM9/2/10
to
"Stephen " <ste...@reynolds.ph.man.ac.uk> wrote in message <i5on8u$43g$1...@fred.mathworks.com>...

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

0 new messages