While using save command, I wanna choose directory which files can be saved into it.
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 22 Mai 2013
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');
1 Kommentar
sermet
am 22 Mai 2013
Kategorien
Mehr zu Environment and Settings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!