Save generated text file to user specified path

조회 수: 12 (최근 30일)
Doug Drake
Doug Drake 2023년 1월 11일
댓글: Les Beckham 2023년 1월 11일
I have a script that generates some data and outputs it to a text file. I want to modify it so it allows the user to save directly to whatever folder they want to save it in, rather than just have it save in the folder with the script. Here is the section that generates the .txt file.
T = [x,y];
fileID = fopen('output_data.txt','w');
fprintf(fileID,'%6s %12s\n','x','y');
fprintf(fileID,'%6.2f %12.8f\n',T);
fclose(fileID);

채택된 답변

Les Beckham
Les Beckham 2023년 1월 11일
편집: Les Beckham 2023년 1월 11일
You can use uigetdir to open a dialog and allow the user to browse for the folder in which to save the file. Read the documentation (which includes examples) here: uigetdir
Then you would modify your fopen call to specify that the file will be in the selected folder. So, something like this:
outputPath = uigetdir
fileID = fopen(fullfile(outputPath, 'output_data.txt'), 'w');
  댓글 수: 2
Doug Drake
Doug Drake 2023년 1월 11일
Thank you, this should be exactly what I need.
Les Beckham
Les Beckham 2023년 1월 11일
You are quite welcome.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by