How to save one variable from one .mat file to another .mat file

12 ビュー (過去 30 日間)
xiao wei
xiao wei 2020 年 10 月 2 日
コメント済み: Ameer Hamza 2020 年 11 月 5 日
There are many.mat file (1.mat; 2.mat; 3.mat...), all these files have many variables, however, all these file contains one variable name 'data' (different values in different file). I hope to change the variable name ('data' ) into '1_data.mat, 2_data.mat, 3_data.mat... 'and save these files into another folder. I appreciate it if someone give me some advice. Thanks in advance.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 2 日
Something like this
files = dir('*.mat');
folder_path = 'C:\...'; % path to folder where you want to save the new files
for i = 1:numel(files)
old_name = files(i).name;
new_name = strrep(old_name, '.mat', '_data.mat');
new_name = fullfile(folder_path, new_name);
data = load(old_name);
data = data.data;
save(new_name, 'data');
end
  4 件のコメント
xiao wei
xiao wei 2020 年 10 月 27 日
now, the code run well. thank you very much.
Ameer Hamza
Ameer Hamza 2020 年 11 月 5 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by