I am facing an error "invalid file identifier" while using fprintf and fopen

1 view (last 30 days)
in my script i am trying to store my result variable using the following line of code,
if(ledaOrLocal ==0)
sdir = 'F:/MS Thesis/Data/Dataset/';
resultDir = 'F:/MS Thesis/Data/Results/';
outputFileName = [resultDir, 'ResultsForParameterTrainingV1To_', num2str(kernel_param.idataset), '.txt'];
file_1 = fopen(outputFileName,'w');
for version = myStart:totalVersions
disp('-------------------------------------------------');
fprintf(file_1,'\n');
fclose(file_1);
but facing the issue of "invalide identifier, the error snap is attached, how it could be solved, help is highly appreciated. thanks

Answers (1)

Jan
Jan on 11 Jan 2023
Whenever you try to open a file, check the success:
[file_1, msg] = fopen(outputFileName, 'w');
assert(file_1 > 0, msg);
Now you see an error message, if the file cannot be opened.
Maybe the directory is not existing or you do not have write permissions in this folder.
Use fullfile instead of concatenating the char arrays:
outputFileName = [resultDir, 'ResultsForParameterTrainingV1To_', num2str(kernel_param.idataset), '.txt'];
% Or safer:
outputFileName = fullfile(resultDir, sprintf('ResultsForParameterTrainingV1To_%d.txt', kernel_param.idataset));

Categories

Find more on Install Products in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!