Open multiple files with fopen

I have written two codes in matlab:
The first one is:
files=dir('*.csv');
for i=1:length(files)
fid(i)=fopen(files(i).name);
files(i).values=textscan(fid(i), '%f %f','delimiter',',','HeaderLines',20,'MultipleDelimsAsOne',1);
end
and the second one is:
str='C:\Users\user\Measurements\Test';
folder_name=uigetdir(str);
files=dir(fullfile(folder_name,'*.csv'));
for i=1:length(files)
fid(i)=fopen(files(i).name);
files(i).values=textscan(fid(i), '%f %f','delimiter',',','HeaderLines',20,'MultipleDelimsAsOne',1);
end
The first code is saved in the same folder as the files that I want to open. The second code is saved outside the folder where the files are stored. But both should read the same files. In the first code the structure files.values is composed by a 1x2 cell. Both cells contain a column vector with numerical values The problem arises in the second code which sometimes gives me an error because fid=-1 or the column vectors in the structure files.values are empty.
How can I overcome this problem?

Respuestas (3)

Walter Roberson
Walter Roberson el 26 de Abr. de 2013

0 votos

Is there a reason to keep the fid's around? Often you would fclose() right after the textscan. This can be important in preventing you from running out of files.
When you do the fopen() ask for the message output. Then test the fid and if it is -1, display the message (and the file name.)
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 26 de Abr. de 2013
Editada: Giorgos Papakonstantinou el 26 de Abr. de 2013

0 votos

Ok I corrected and I included the fclose() but I still get empty cells in the structure files.values. Why is that?
And also when I requested for a message I got this:
No such file or directory
which I can not explain it since the folder contains *.csv files and the folder exists since I use the uigetdir and I select the folder from the browser myself.

5 comentarios

When I try to debug it. The command:
files=dir(fullfile(folder_name,'*.csv'));
creates structure named 'files'. Inside the structure 'files' there is a 'files.name' which has the assigned string 'test.csv', which is the file that I want to open.
But in the next step the command
[fid(i),msg]=fopen(files(i).name);
gives a message
No such file or directory
and frankly I cannot understand because it exists in the workspace!!
Another weird thing that I cannot explain happens when I use the command
[fid(i),msg]=fopen(files(i).name,'wt+');
In this case the fid takes a value different from -1 and the subsequent fopen command can be executed but then files.values is empty!!
Giorgos Papakonstantinou
Giorgos Papakonstantinou el 26 de Abr. de 2013
At last I have noticed that if I move the second code within the folder that csv files are contained then code does not exhibit any problem. All the numerical values are contained in files.values structure.
Kostas Chatzimpaloglou
Kostas Chatzimpaloglou el 16 de Abr. de 2020
have found a solution to this problem?
[fid(i),msg]=fopen(files(i).name);
should be
[fid(i),msg]=fopen( fullfile(folder_name, files(i).name));

Iniciar sesión para comentar.

Giorgos Papakonstantinou
Giorgos Papakonstantinou el 26 de Abr. de 2013

0 votos

Ok solved myself. The problem was that I was using textscan outside the current folder. Therefore I changed the folder to the folder of interest and back to the initial folder.
str='C:\Users\user\Measurements\Test';
folder_name=uigetdir(str);
files=dir(fullfile(folder_name,'*.csv'));
curr_folder=pwd;
cd(folder_name);
for i=1:length(files)
[fid(i),msg]=fopen(files(i).name);
disp(msg);
files(i).values=textscan(fid(i), '%f %f','delimiter',',',...
'Headerlines',20,...
'MultipleDelimsAsOne',1);
fclose(fid(i));
end
cd(curr_folder)

1 comentario

Stephen23
Stephen23 el 17 de Abr. de 2020
Using cd should be avoided in code.
Using absolute/relative filenames is more efficient, robust, simple, and easier to debug, e.g.:
D = 'C:\Users\user\Measurements\Test';
S = dir(fullfile(D,'*.csv'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
[fid,msg] = fopen(F,'rt');
assert(fid>=3,msg)
S(k).values = textscan(fid,...);
fclose(fid);
end

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 26 de Abr. de 2013

Comentada:

el 17 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by