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

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

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

First of all, thank you for your time and your disponibility. I use your code, exatcly like you have wrote. The error message now is:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in load_def (line 34)
AllResult = [AllResult;A1];
that's because you have some files that contain data other than 90x1 vector.
when you get this error, what is the variable A1 ? type ? size ?
if you have files with different sizes of vectors, just change
AllResult = [AllResult;A1];
with
AllResult{end+1,1} = A1;
this way, you'll get a cell array, with vector of different sizes.
Ok, I change this last thing and the program is ok!!
But, let me see if I understand: now I have Allresults that is a vector column with 4418 cells in which I have 90 values for each cell. Is right?
what you should have is :
NumberOfNumericalFolder = length(MyStringDir)
NumberOfRun = 2
NumberofTextFiles = 85
TotalNumber = NumberOfNumericalFolder * NumberOfRun * NumberofTextFiles
TotalNumber =
4420
if not, it means you have one or two folder in which there are some missing files, or some files have not been read, but you should have received some warnings.
the is each cell a 90x1 vector. I have no idea. if all your files contain just 90 numerical data, then yes, otherwise, in some cells you're gonna get something else.
if you want to check
for i = 1:size(AllResult,1)
if ~isequal(size(AllResult{i}),[1 90])
disp(i)
disp(size(AllResult{i}))
end
end
that will display the element that contain something with a size different than 1x90
Ok. In theory I have 26 folders, with 2 run for each folder. Inside each run I have 85 file (.txt) in wich I have 90 values.
I found that in one of 26 folders I have only 84 files for each run (and not 85). So finally I don't have 4420 cells but only 4418. But it is another (and easy) problem. Thank you very much for your help.
And this is exactly why I advised you to use dir to get the list of files rather than use a hardcoded list. Sooner or later, the hardcoded list won't match the actual files on disk.
Using dir may be slightly more complicated to write to start with, but ultimately will lead to more robust code that doesn't stop just because a file is missing.
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)

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'};
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

fopen didn't success to open the file, that's why the output fid is set to -1 (failed to open).
outside your loopr, try to open a single file, to see if it works. in the command window :
file = 'C:\Temporal_series\30852\run1\ts_onto3D_aparc+aseg_Thalamus-Proper.nii.gz';
fid_1 = fopen(file,'r+');
A1 = fscanf(fid_1,'%f %f', [90 1]);
A1 = A1';
fclose(fid_1);
if you get fid_1 = -1, then it failed to open the file.
Also, are you sure of your file's extensions '.nii.gz'. seems weird ?
I tried to open a single file, but it failed (fid_1 is still =-1). The files that I use are file .txt The extension .nii.gz is not an extension but only part of name of file. I attach to this message one of 85 files, so you can see which kind of file I use.
Your extension is .txt. I thought it was .gz.
so, it's normal you get an error, because fopen is trying to open a file with the extension .gz, which is not the correct one.
2 ways to make it work, pick the one you prefer:
1) modify the definition of list_ROI to add the .txt in every name :
list_ROI = {'ts_onto3D_aparc+aseg_Left-Cerebellum-Cortex.nii.gz.txt';
'ts_onto3D_aparc+aseg_Thalamus-Proper.nii.gz.txt';
'ts_onto3D_aparc+aseg_Left-Caudate.nii.gz.txt';
....
2) add the .txt extension in the sprinf command
file = sprintf('C:\\Temporal_series\\%d\\run1\\%s.txt',list_exam(i),list_ROI{j});
I would pick the first one, because you'll get all the information in one location. name and extension defined in the one data list_ROI
I would pick neither and use dir to get the list of files in the relevant directory. That way you're sure your filelist matches the files that actually exist.
Ok, I changed every name in list_ROI and now I have the right extension for my file. But when I run my code, now the loop stops at i=1 and j=2 (stops at the secodn file of the list_ROI). And also if I try to open one file at a time, in the command window, the first returns ok but the second returns fid=-1.
I really don't understand the problem.
Hi Guillaume, thank you. Please, let me see how can I use 'dir'

Sign in to comment.

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

Ok, I tried with you suggestion, and I wrote this
% Loop for run1
for i=1:26
for j=1:85
file = sprintf('C:\\Temporal_series\\%d\\run1\\%s',list_exam(i),list_ROI{j});
[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
fid_1 = fopen(file,'r+');
A1 = fscanf(fid_1,'%f %f', [90 1]);
A1 = A1';
fclose(fid_1);
end
end
end
But it doesn't work. Any ideas?
What does it doesn't work mean?
What error, if any, do you get?
Sorry, you are right. If i write 'error' instead of 'warning', the error messsage is
??? Error using ==> load_txt at 100 failed to open C:\Temporal_series\30852\run1\ts_onto3D_aparc+aseg_Thalamus-Proper.nii.gz.txt due to No such file or directory
Do you know why? And another question, why the error occurs on the second file of the list and not at the first (ts_onto3D_aparc+aseg_Left-Cerebellum-Cortex.nii.gz)?
Regarding your comment to your "Answer" about how to use dir(), see the FAQ for robust code examples: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
If you want to use dir, the following possibly works, assuming you want to process all directories within Temporal_series and all txt files within run1:
pathroot = 'C:\Temporal_series';
allfiles = dir(fullfile(pathroot, '*')); %use fullfile rather than sprintf to build paths
validdirs = {allfiles([allfiles.isdir] & ~(strcmp({allfiles.name}, '.') | strcmp({allfiles.name}, '..'))).name};
for examdir = validdirs
currentpath = fullfile(pathroot, exmadir{1}, 'run1');
txtfiles = dir(fullfile('currentpath', '*.txt'));
for filename = {txtfiles.name}
[fid, errormsg] = fopen(fullfile(currentpath, filename{1}), 'r+');
if ~isempty(errmsg)
warning('failed to open %s due to %s', file, errmsg);
else
%process as normal
fclose(fid);
end
end
end
If you get an error on the second file and not the first, it's possibly because the first one exists, but not the second. Maybe you've typed it wrong, or it's in another directory, or ...
Hence, why I recommended using dir.
If I write this
% Loop for run1 for i=1:26 for j=1:85
pathroot = 'C:\Temporal_series';
allfiles = dir(fullfile(pathroot, '*')); %use fullfile rather than sprintf to build paths
validdirs = {allfiles([allfiles.isdir] & ~(strcmp({allfiles.name}, '.') | strcmp(allfiles.name), '..'))).name};
for examdir = validdirs
currentpath = fullfile(pathroot, exmadir{1}, 'run1');
txtfiles = dir(fullfile('currentpath', '*.txt'));
for filename = {txtfiles.name}
[fid, errormsg] = fopen(fullfile(currentpath, filename{1}), 'r+');
if ~isempty(errmsg)
warning('failed to open %s due to %s', file, errmsg);
else
%process as normal
fid_1 = fopen(file,'r+');
A1 = fscanf(fid_1,'%f %f', [90 1]);
A1 = A1';
fclose(fid);
end
end
end
end
end
I have errors in line 'validdirs=....'. Look at the attach (error.png) to undertand where the error is. Thank you for your patience
Sorry, should have been:
validdirs = {allfiles([allfiles.isdir] & ~(strcmp({allfiles.name}, '.') | strcmp({allfiles.name}, '..'))).name};
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.

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

"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.

Categories

Asked:

on 10 Nov 2014

Commented:

on 9 Feb 2022

Community Treasure Hunt

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

Start Hunting!