??? Error using ==> fscanf. Invalid file identifier. Use fopen to generate a valid file identifier.

90 views (last 30 days)
Lorenzo
Lorenzo on 10 Nov 2014
Commented: Stephen23 on 9 Feb 2022
Hi to all. I have this kind of problem: I need to load into a 3D matrix about 4.000 files, that were into 26 different directory, and that files are 85 for each of this 26 directory (I have 26 directory, into every directory I have two subdirectory "run1" and "run2" and inside this two subdirectory I have 85 files). I create two lists, one list of numbers and one list of characters. This is my code:
% Initialization vectors Esami and ROIs
list_exam = [30852 22061 20769 21734 21735 21977 20856 21976 20086 30697 30630 19993 30018 28832 19725 22440 28333 22439 22587 22586 21403 30944 21405 30943 22337 30948];
list_ROI = ['ts_onto3D_aparc+aseg_Left-Cerebellum-Cortex.nii.gz ';
'ts_onto3D_aparc+aseg_Thalamus-Proper.nii.gz ';
'ts_onto3D_aparc+aseg_Left-Caudate.nii.gz ';
'ts_onto3D_aparc+aseg_Left-Putamen.nii.gz ';
'ts_onto3D_aparc+aseg_Left-Pallidum.nii.gz ';
'ts_onto3D_aparc+aseg_Brain-Stem.nii.gz ';
'ts_onto3D_aparc+aseg_Left-Hippocampus.nii.gz ';
'ts_onto3D_aparc+aseg_Left-Amygdala.nii.gz ';
'ts_onto3D_aparc+aseg_Left-Accumbens-area.nii.gz ';
'ts_onto3D_aparc+aseg_Left-VentralDC.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Cerebellum-Cortex.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Thalamus-Proper.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Caudate.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Putamen.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Pallidum.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Hippocampus.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Amygdala.nii.gz ';
'ts_onto3D_aparc+aseg_Right-Accumbens-area.nii.gz ';
'ts_onto3D_aparc+aseg_Right-VentralDC.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-caudalanteriorcingulate.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-caudalmiddlefrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-cuneus.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-entorhinal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-fusiform.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-inferiorparietal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-inferiortemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-isthmuscingulate.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-lateraloccipital.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-lateralorbitofrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-lingual.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-medialorbitofrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-middletemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-parahippocampal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-paracentral.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-parsopercularis.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-parsorbitalis.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-parstriangularis.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-pericalcarine.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-postcentral.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-posteriorcingulate.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-precentral.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-precuneus.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-rostralanteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-rostralmiddlefrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-superiorfrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-superiorparietal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-superiortemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-supramarginal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-frontalpole.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-temporalpole.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-transversetemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-lh-insula.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-caudalanteriorcingulate.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-caudalmiddlefrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-cuneus.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-entorhinal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-fusiform.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-inferiorparietal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-inferiortemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-isthmuscingulate.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-lateraloccipital.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-lateralorbitofrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-lingual.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-medialorbitofrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-middletemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-parahippocampal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-paracentral.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-parsopercularis.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-parsorbitalis.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-parstriangularis.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-pericalcarine.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-postcentral.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-posteriorcingulate.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-precentral.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-precuneus.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-rostralanteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-rostralmiddlefrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-superiorfrontal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-superiorparietal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-superiortemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-supramarginal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-frontalpole.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-temporalpole.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-transversetemporal.nii.gz ';
'ts_onto3D_aparc+aseg_ctx-rh-insula.nii.gz '];
% Initialization matrix 3-D
A1 = zeros(26,85,90);
% Loop for run1
for i=1:26
for j=1:85
file = 'C:\Temporal_series\list_exam[i]\run1\list_ROI[j]';
fid_1 = fopen(file,'r+');
A1 = fscanf(fid_1,'%f %f', [90 1]);
A1 = A1';
fclose(fid_1);
end
end
Now, the error is :
??? Error using ==> fscanf Invalid file identifier. Use fopen to generate a valid file identifier.
I think that the problem is about the vectors index inside path of fopen.
Someone can help me? Thank you

Accepted Answer

Orion
Orion on 10 Nov 2014
try this :
%%Root Path
pathroot = 'C:\Temporal_series';
%%first level folder
MyNumericalDir = [30852 22061 20769 21734 21735 21977 20856 21976 20086 30697 30630 19993 30018 28832 19725 22440 28333 22439 22587 22586 21403 30944 21405 30943 22337 30948];
% convert it to string : easier to treat as folder Names.
MyStringDir = cellfun(@num2str,num2cell(MyNumericalDir),'UniformOutput',false);
% Initialize The Outut Data (which will contain all the results (change the format if you prefer)
% Here, I assume all the files containts a 90x1 vector, so i will
% concatenate to create an array.
AllResult = [];
%%loop on every "numerical" folder
for i = 1:length(MyNumericalDir)
%%get all ".gz.txt" in the run1 folder
CurrentDir = fullfile(pathroot,MyStringDir{i},'run1');
AllFile = dir(fullfile(CurrentDir,'*gz.txt'));
% loop for each file
for j = 1:size(AllFile,1)
% current file
CurrentFile = fullfile(CurrentDir,AllFile(j).name);
% try to open
[fid, errormsg] = fopen(CurrentFile, 'r+');
if ~isempty(errormsg)
warning('failed to open %s due to %s', CurrentFile, errormsg);
else
A1 = fscanf(fid,'%f %f', [90 1]);
A1 = A1';
AllResult = [AllResult;A1];
fclose(fid);
end
end
%%same operation for run2 (copy/paste, I just changed run1 in run2)
CurrentDir = fullfile(pathroot,MyStringDir{i},'run2');
AllFile = dir(fullfile(CurrentDir,'*gz.txt'));
% loop for each file
for j = 1:size(AllFile,1)
% current file
CurrentFile = fullfile(CurrentDir,AllFile(j).name);
% try to open
[fid, errormsg] = fopen(CurrentFile, 'r+');
if ~isempty(errormsg)
warning('failed to open %s due to %s', CurrentFile, errormsg);
else
A1 = fscanf(fid,'%f %f', [90 1]);
A1 = A1';
AllResult = [AllResult;A1];
fclose(fid);
end
end
end
  7 Comments
Lorenzo
Lorenzo on 21 Nov 2014
Hi Orion,
I think I could need your help again.
I posted another question about cell array ('Problem with average two cells array'). Please, take a look.
Thank you so much

Sign in to comment.

More Answers (4)

Orion
Orion on 10 Nov 2014
Edited: Orion on 10 Nov 2014
Hi Lorenzo,
You're trying to read a file named literally
file = 'C:\Temporal_series\list_exam[i]\run1\list_ROI[j]';
this file does not exist. The error is not with fscanf, but with fopen which tries to open a file that doesn't exist.
what you want is :
for i=1:26
for j=1:85
file = sprintf('C:\\Temporal_series\\%d\\run1\\%s',list_exam(i),list_ROI{j});
fid_1 = fopen(file,'r+');
A1 = fscanf(fid_1,'%f %f', [90 1]);
A1 = A1';
fclose(fid_1);
end
end
Note I change the definition of your data list_ROI by making it a cell array. it's cleaner to use (no need to define multiple space to concatenate string array.)
list_exam = [30852 22061 20769 21734 21735 21977 20856 21976 20086 30697 30630 19993 30018 28832 19725 22440 28333 22439 22587 22586 21403 30944 21405 30943 22337 30948];
list_ROI = {'ts_onto3D_aparc+aseg_Left-Cerebellum-Cortex.nii.gz';
'ts_onto3D_aparc+aseg_Thalamus-Proper.nii.gz';
'ts_onto3D_aparc+aseg_Left-Caudate.nii.gz';
'ts_onto3D_aparc+aseg_Left-Putamen.nii.gz';
'ts_onto3D_aparc+aseg_Left-Pallidum.nii.gz';
'ts_onto3D_aparc+aseg_Brain-Stem.nii.gz';
'ts_onto3D_aparc+aseg_Left-Hippocampus.nii.gz';
'ts_onto3D_aparc+aseg_Left-Amygdala.nii.gz';
'ts_onto3D_aparc+aseg_Left-Accumbens-area.nii.gz';
'ts_onto3D_aparc+aseg_Left-VentralDC.nii.gz';
'ts_onto3D_aparc+aseg_Right-Cerebellum-Cortex.nii.gz';
'ts_onto3D_aparc+aseg_Right-Thalamus-Proper.nii.gz';
'ts_onto3D_aparc+aseg_Right-Caudate.nii.gz';
'ts_onto3D_aparc+aseg_Right-Putamen.nii.gz';
'ts_onto3D_aparc+aseg_Right-Pallidum.nii.gz';
'ts_onto3D_aparc+aseg_Right-Hippocampus.nii.gz';
'ts_onto3D_aparc+aseg_Right-Amygdala.nii.gz';
'ts_onto3D_aparc+aseg_Right-Accumbens-area.nii.gz';
'ts_onto3D_aparc+aseg_Right-VentralDC.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-caudalanteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-caudalmiddlefrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-cuneus.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-entorhinal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-fusiform.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-inferiorparietal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-inferiortemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-isthmuscingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-lateraloccipital.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-lateralorbitofrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-lingual.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-medialorbitofrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-middletemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-parahippocampal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-paracentral.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-parsopercularis.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-parsorbitalis.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-parstriangularis.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-pericalcarine.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-postcentral.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-posteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-precentral.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-precuneus.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-rostralanteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-rostralmiddlefrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-superiorfrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-superiorparietal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-superiortemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-supramarginal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-frontalpole.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-temporalpole.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-transversetemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-lh-insula.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-caudalanteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-caudalmiddlefrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-cuneus.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-entorhinal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-fusiform.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-inferiorparietal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-inferiortemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-isthmuscingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-lateraloccipital.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-lateralorbitofrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-lingual.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-medialorbitofrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-middletemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-parahippocampal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-paracentral.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-parsopercularis.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-parsorbitalis.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-parstriangularis.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-pericalcarine.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-postcentral.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-posteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-precentral.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-precuneus.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-rostralanteriorcingulate.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-rostralmiddlefrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-superiorfrontal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-superiorparietal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-superiortemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-supramarginal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-frontalpole.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-temporalpole.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-transversetemporal.nii.gz';
'ts_onto3D_aparc+aseg_ctx-rh-insula.nii.gz'};

Lorenzo
Lorenzo on 10 Nov 2014
Hi Orion Thank you for your answer. I change my code with your suggestions but the error message is still present.
??? Error using ==> fscanf Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> load_txt at 98
A1 = fscanf(fid_1,'%f %f', [90 1]);
The loop starts and stops at i=1 and j=1.
The variable fid is =-1.
You know what is wrong now? Thank you very much.
  6 Comments

Sign in to comment.


Guillaume
Guillaume on 10 Nov 2014
The most likely reason for this sort of error is because fopen failed to open the file, either because you supplied the wrong filename, or permissions are wrong, or the file is open exclusively by another program, or any other reason.
The first two can be solved easily, supply the correct name / edit the permissions. The others not so much. So I would suggest you make your program resistant to such failures by changing your code to:
[fid_1, errmsg] = fopen(file, 'r+);
if ~isempty(errmsg)
warning('failed to open %s due to %s', file, errmsg); %or use error if you want the code to stop
else
%process the file as normal
end
  9 Comments
Lorenzo
Lorenzo on 10 Nov 2014
Ok, I changed this last thing, and the program goes without errors (now I used two different ways, yours and the idea of Orione, cause two is better than one!). But the variable A1 at the end is a 3D matrix with all zeros. Why there aren't my values inside matrix?

Sign in to comment.


Amanda Eriksson
Amanda Eriksson on 9 Feb 2022
I know that this question was originally asked several years ago, but since I ended up with a similair error message and went through this tread I just wanted to insert for future coders that it can be wise to also look if you have added the path for where you have stored your data.
Sometimes it can be a verry simple error one have made, and therefore the fid returns a -1 due to that it does't have access to that pathway where the data is located.
  1 Comment
Stephen23
Stephen23 on 9 Feb 2022
"it can be wise to also look if you have added the path for where you have stored your data."
No, the path to data files should not be added to the MATLAB search path. The search path is for code files.
To access data files simply use an absolute/relative filename (for which FULLFILE is very useful):

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by