How to print some text in a txt file in another folder?
1 次查看(过去 30 天)
显示 更早的评论
Dear All,
Hope this message finds you all being safe and healthy.
I am trying to print some text and save them in a txt file in another folder. My code is as follows:
if ~exist('Solutions', 'dir')
mkdir('Solutions')
end
fileID = fopen('GeneralResults.txt','w');
fprintf(fileID, ' ###################################################################');
fprintf(fileID, ' # #');
fprintf(fileID, ' # The code is finished on May 08, 2020. #');
fprintf(fileID, ' # #');
fprintf(fileID, ' ###################################################################');
fclose(fileID);
I run the above code, but I found the result is Not correct:
1). The file I created "GeneralResults.txt' was Not saved in the folder 'Solutions', but in current folder.
I will really appreciate it if you can help me to fix these two problems. Thanks a lot.
Best regards,
Benson
1 个评论
Sulaymon Eshkabilov
2020-5-8
Hi,
You are almost there except for one missing point, see below:
if ~exist('Solutions', 'dir')
mkdir('Solutions')
cd('./Solutions') % Add the command to change the directory
end
采纳的回答
更多回答(2 个)
Sulaymon Eshkabilov
2020-5-8
Hi,
You are almost there except for one missing point, see below:
if ~exist('Solutions', 'dir')
mkdir('Solutions')
cd('./Solutions') % Add the command to change the directory
end
Sulaymon Eshkabilov
2020-5-8
Please try this code:
remove the created folder solutions:
if ~exist('Solutions', 'dir')
mkdir('Solutions')
cd('./Solutions')
end
fileID = fopen('GeneralResults.txt','w');
fprintf(fileID, ' ###################################################################');
fprintf(fileID, ' # #');
fprintf(fileID, ' # The code is finished on May 08, 2020. #');
fprintf(fileID, ' # #');
fprintf(fileID, ' ###################################################################');
fclose(fileID);
% I have tested it is working perfectly well
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!