fopen function how to use a variable name to select a file present in a folder?

i have some csv files in a folder which i am trying to edit using matlab.
my code is below
yourfolder='D:\practice\Matlab\APM3 magnet measurements\Sensormagnet1';
d=dir([yourfolder '\*.csv']);
files={d.name};
file=files(1);
fid = fopen(file,'w');
files is a variable which has list of csv files present in folder. i created a variable to get one file name out of the list and trying to open the file with the name present in the file variable.
its giving error as given below:
Error using fopen
First input must be a file name of type char, or a file identifier of type double.
Error in CSV (line 5)
fid = fopen(file,'w');
please help me on this

 Accepted Answer

The mistake is on this line:
file=files(1);
You need to use cell array indexing, then the code will work:
file = files{1};

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Asked:

on 1 Jun 2016

Edited:

on 2 Jun 2016

Community Treasure Hunt

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

Start Hunting!