How to save a data in another folder?

Hello,
I have a folder name BSL_results and want to save following results into that that folder each time I run a code:
The folder BSL_results is in the same folder where code is being run. and want to save follwing two files (below) there. The .mat files are trc1.mat and s1.mat
sw_1 = Swf; Pe_1 = Pef; % inverted
sw0_1 = Sw0; Pe0_1 = Pe0;
Snear1 = data(:,1); Smid1 = data(:,2); Sfar1 = data(:,3);
save s1.mat sw_1 sw0_1 Pe_1 Pe0_1;
save trc1.mat Snear1 Smid1 Sfar1;

 採用された回答

Jim Riggs
Jim Riggs 2022 年 11 月 9 日
編集済み: Jim Riggs 2022 年 11 月 9 日

0 投票

I like to define a string defining the work folder; e.g.
workfolder = 'C:/user/JR/projectName/'
save([workfolder 's1.mat'], sw_1, sw0_1, Pe_1, Pe0_1 )
save([workfolder 'trc1.mat'], Snear1, Smid1, Sfar1)
This way, the file is saved in the specified location no mater what the Matlab working directory is.

4 件のコメント

Voss
Voss 2022 年 11 月 9 日
編集済み: Voss 2022 年 11 月 9 日
You should have quotes around the variable names in the calls to save, i.e.:
save([workfolder 's1.mat'], 'sw_1', 'sw0_1', 'Pe_1', 'Pe0_1' )
save([workfolder 'trc1.mat'], 'Snear1', 'Smid1', 'Sfar1')
Jim Riggs
Jim Riggs 2022 年 11 月 10 日
Thank you for the correction.
Image Analyst
Image Analyst 2022 年 11 月 10 日
Further correction (use fullfile):
save(fullfile(workfolder, 's1.mat'), 'sw_1', 'sw0_1', 'Pe_1', 'Pe0_1' )
save(fullfile(workfolder, 'trc1.mat'), 'Snear1', 'Smid1', 'Sfar1')
Nisar Ahmed
Nisar Ahmed 2022 年 11 月 10 日
@Image Analyst thanks for correction and it is working

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSearch Path についてさらに検索

質問済み:

2022 年 11 月 9 日

コメント済み:

2022 年 11 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by