Saving outputs to different folder than m-file.

조회 수: 85 (최근 30일)
Daniel
Daniel 2013년 6월 28일
댓글: Stephen23 2017년 1월 25일
I have a script that I use on many different files saved in different folders. All of the files I want to keep separate I keep in different folders. I use the 'save' and 'saveas' functions in my script to keep my results. I have only one version of the script at a time, and change the inputs as appropriate for different data sets.
I would like to have the saved files to be save in the same folder I am pulling the inputs from, instead of the folder I have the m-file saved in. At this point I have not been able to successfully do this.

채택된 답변

Matt Kindig
Matt Kindig 2013년 6월 28일
%get the directory of your input files:
pathname = fileparts('/input/file');
%use that when you save
matfile = fullfile(pathname, 'output.mat');
figfile = fullfile(pathname, 'output.fig');
save(matfile, ...');
saveas(figfile, ...');
  댓글 수: 1
Daniel
Daniel 2013년 6월 28일
Thank you. Works just as needed. I keep forgetting to connect multiple parts of strings together, like this for a file name.

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

추가 답변 (2개)

Peyman Obeidy
Peyman Obeidy 2017년 1월 25일
Thank you, this will help you to create a new folder and save the outcome into the new folder
% code
%make a new directory for each file
%make
mkdir(num2str(pName),fName(1:length(fName)-4));
% get the new path
FolderDestination=strcat(num2str(pName),fName(1:length(fName)-4));
% make a mfile in new path
matfile = fullfile(FolderDestination, 'output.mat');
% add variables into that m file
save(matfile);
  댓글 수: 1
Stephen23
Stephen23 2017년 1월 25일
There some significant improvements that could be made to this code. For a start, this code assumes that file extensions all have the same length (not true). A better solution is to simply use
[pathstr,namestr,extstr] = fileparts(fname);
which will correctly identify and separate the path, name, and extensions, regardless of the length of the extension. Also the this code assumes that PName has a trailing file separator character (might not be true). A much better solution than using strcat is to use:
fullfile(pathstr,[filestr,extstr])
which will always check and include the file separator character if required.

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


Peyman Obeidy
Peyman Obeidy 2017년 1월 25일
[fName, pName] = uigetfile('*.tif');

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by