0

I have been trying to open files in a loop. I did this:

file='';
loc='F:\UT_timestep\';
name='time_';
gridext='.grd';
for i={'a','b','c'}
   file=strcat(loc,name,i,gridext);
   f=fopen(file,'rb');
   ...
   fclose(f);
end

but it gives this error :

Error using fopen
First input must be a file name of type char, or a file identifier of type double.

Error in script_UT (line 28) f=fopen(file,'rb');

I am not able to understand why this is giving error. Please help.

0

1 Answer 1

2

This is because file is a cell array of 1 element. You want the actual string inside the cell array, not the actual cell itself. Do this:

file='';
loc='F:\UT_timestep\';
name='time_';
gridext='.grd';
for i={'a','b','c'}
   file=strcat(loc,name,i,gridext);
   f=fopen(file{1},'rb'); %// Change
   ...
   fclose(f);
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.