problem with overwritten data when auto saving

2 ビュー (過去 30 日間)
Lizan
Lizan 2014 年 10 月 16 日
回答済み: Matt Tearle 2014 年 10 月 16 日
I want to auto save certain data based on where they are located in the computer. For example,
myFolder = '/Users/user/Documents/MATLAB - my measurements/16.10.2014/785/happy/nr/';
I want to save the data in a certain directory;
savdir = '/Users/user/Documents/MATLAB - my measurements/results/';
So I would write
diary(fullfile(savdir,['file_',mfilename,myFolder(53:62),'_diary.txt']))
diary on
-- some code
diary off
save(fullfile(savdir,['file_',mfilename,myFolder(53:62),'_data.mat']));
savefig(h,fullfile(savdir,['file_',mfilename,myFolder(53:62),'_plots.fig']));
close(h)
But I have several folders with data in folder happy, called nr, nr1, nr2 etc or my, my1, my2 etc.
So saving data from folder nr and then nr2 will overwrite the previously data with current code. Is there a way to not overwrite?
I guess I would write for
myFolder = '/Users/user/Documents/MATLAB - my measurements/16.10.2014/785/happy/nr2/';
myFolder = '/Users/user/Documents/MATLAB - my measurements/16.10.2014/785/happy/nr/';
for example
save(fullfile(savdir,['file_',mfilename,myFolder(53:62),myFolder(end-4:end-1),'_data.mat']));
but as you can see above this is not always the case that 'nr' has a number in front of it.
Any suggestions?
  1 件のコメント
Lizan
Lizan 2014 年 10 月 16 日
Just like to comment that MATLAB seem to have problems with "/" sign in this.

サインインしてコメントする。

採用された回答

Matt Tearle
Matt Tearle 2014 年 10 月 16 日
I'm not entirely clear on what the crux of the problem is. Are you trying to loop through the directories (nr, nr2, nr3) and want to create the directory name programmatically?
It seems like ls (and cellstr) should help:
nrDirs = cellstr(ls([pathstuff,'nr*']));
Then use that
save(fullfile(...,nrDirs{k},'_data.mat']))
Also, regarding your comment about /, use filesep to get the correct file system separator for your OS (/ or \). If you don't want to mess with the hardcoded paths you've already made, you could even do
myFolder = regexprep(myFolder,'/',filesep); % replace all /s with either / or \

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Name Construction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by