필터 지우기
필터 지우기

How to print some text in a txt file in another folder?

조회 수: 2 (최근 30일)
Benson Gou
Benson Gou 2020년 5월 8일
댓글: Benson Gou 2020년 5월 8일
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
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
Sulaymon Eshkabilov 2020년 5월 8일
use:
cd('../')
That moves one level up.
Don't forget to hit accept :)

추가 답변 (2개)

Sulaymon Eshkabilov
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
  댓글 수: 1
Benson Gou
Benson Gou 2020년 5월 8일
Thanks, Sulaymon,
I tried to add the missing sentence in my code, but I found the file GeneralResults.txt is till in current folder except the folder of 'Solutions'.
But thank you anyway.
Benson

댓글을 달려면 로그인하십시오.


Sulaymon Eshkabilov
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
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 5월 8일
note that there is "." before /Solutions ... :)
Benson Gou
Benson Gou 2020년 5월 8일
Yes, it works very well. I made a mistake. Thanks a lot.
By the way, how can I go back to the previous folder after I finish the print?
Best regards,
Benson

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by