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

조회 수: 6 (최근 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 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