While using save command, I wanna choose directory which files can be saved into it.
이전 댓글 표시
for example;
p = rand(1, 10)
q = ones(10)
save('pqfile.txt', 'p', 'q', '-ASCII') % in here I cannot choose the directory which pqfile will save into it. I wanna save this file another directory, can I choose a different path?
채택된 답변
추가 답변 (1개)
Image Analyst
2013년 5월 22일
A fairly robust way if you're going to hard code in the folder:
folder = 'C:\Users\sermet\Documents'; % Or wherever
if exist(folder, 'dir') == 0
% Make folder if it does not exist.
mkdir(folder);
end
% Create full filename with the fullfile() function:
fullFileName = fullfile(folder, 'pqfile.txt');
save(fullFileName, 'p', 'q', '-ASCII');
Or, if you're going to have the user specify a file, try it this way:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
save(fullFileName, 'p', 'q', '-ASCII');
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!