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

1 回表示 (過去 30 日間)
Waseem Abbas
Waseem Abbas 2023 年 1 月 11 日
回答済み: Jan 2023 年 1 月 11 日
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

回答 (1 件)

Jan
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));

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by