I am facing an error "invalid file identifier" while using fprintf and fopen
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Jan
2023-1-11
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));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!