Why am I getting an 'Unable to open file.' error with importdata?

Hi,
Patience is requested for the simple question, I haven't used Matlab since undergrad times.
I need to understand how to avoid an error with 'importdata'. Using 'importdata' or 'csvread' I get 'Unable to open file' errors, even when I can see the file being read if I caption 'importdata' with 'eval'.
I tested it with debugger and the filename is resolving correctly. When I use the filename as a string, it works ok. When I use uiimport it works also. Obviously these solutions won't be practical for many repetitions (numreps is in the order of 100s).
Looking at the variable workspace I can see that 'newfile' is actually populated with the correct data.
I am running MATLAB R2011b on OSX 10.8.
for n = 0:numreps
fle = strcat('batchRun', num2str(n,'%03d'), '_crt.csv');
%newfile = importdata(fle);
%newfile = importdata('batchRun000_crt.csv')
eval('newfile = importdata(fle)');
end;
Thanks in Advance,

4 Comments

Are you sure you want %04s and not %04d ?
Correct, i just found that error. Have edited the question.
"eval('newfile = importdata(fle)');"?! Why not:
newfile = importdata(fle);
I'd prefer SPRINTF, but this might be a question of taste:
fle = sprintf('batchRun%03d_crt.cvs', n);
See below in your Answer for comments.

Sign in to comment.

 Accepted Answer

In your posted code, thet variable newfile is overwritten in each iteration. Are you sure that all files are existing?
newFile = cell(1, numreps + 1);
for n = 0:numreps
fle = sprintf('batchRun%03d_crt.cvs', n);
try
newFile{n + 1} = importdata(fle);
catch
fprintf('Cannot read: %s\n', fle);
disp(exist(fullfile(cd, fle)))
end
end
[EDITED] Avoiding errors is better than catching them, so I'd prefer:
folder = cd;
newFile = cell(1, numreps + 1);
for n = 0:numreps
fle = fullfile(folder, sprintf('batchRun%03d_crt.cvs', n));
if exist(fle, 'file') == 2
newFile{n + 1} = importdata(fle);
else
fprintf('Skip missing file: %s\n', fle);
end
end

3 Comments

Yes, that's true - I didn't include any processing of the file, which was to copy values from specific lines of 'newfile' to another array, in a new row for each iteration of the loop. I missed this out for brevity, and because the code was working.
Thanks Jan - sprintf worked without raising an error. note: I used 'eval' because this way i got the array imported before the error was raised.
@Angus: You cannot cheat Matlab to import a file inspite of an error using eval().
Maybe not, but for some reason it worked on my configuration with 'eval'. I didn't understand why and chased it down with the debugger and saw the variable be updated before an error was raised. However I needed to continue the assignment and didn't pursue further.

Sign in to comment.

More Answers (1)

update: I got around this by putting in a try/catch statement which did nothing with the error since the data was being imported to the variable. I still don't know why the error occurs.

Products

Asked:

on 7 Nov 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!