필터 지우기
필터 지우기

Saving workspace variables with different .mat file name using for loop

조회 수: 13 (최근 30일)
Zeab
Zeab 2019년 1월 7일
편집: Zeab 2019년 1월 9일
Hello all,
I just wanted to save workspace variables as a .mat file whose set of variables are extracted from a struct that consists of time and data fields in a timeseries struct and therfore the different variables should be named differently,
i have tried the ff
for i=1:N
temp=x{i,1}.data
save temp.mat temp
end
unfortunatly the above code saves only the last iteration (Nth iteration),any help appreciated to access all the N elements,and eventually creat N math files,
Thank you in Advance!
  댓글 수: 8
Zeab
Zeab 2019년 1월 9일
Have you seen that the above section of code can only generate a new file name for every loop iteration but assigning the data i.e the variable temp to the .mat file fanme can't succesufly implemented.
Zeab
Zeab 2019년 1월 9일
I wonder if you could try the attached ,mat file (Vb1nicw.mat) to check the above section of code.
Thank you in advance!

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 9일
My guess at what you want:
filestruct = load('Vb1nicw.mat');
fn = fieldnames(filestruct);
for K = 1 : length(fn)
thisfield = fn{K};
tempvar = struct(thisfield, filestruct.(thisfield));
outfile = [thisfield '.mat'];
save(outfile, '-struct', 'tempvar');
end
This will take each variable in Vb1nicw.mat and will save it out to a separate .mat file that is named after the variable, and the variable name used inside the .mat file will be the same as the name of the variable.
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 1월 9일
for K=1:length(Vb1nicw)
fn = sprintf('Vb1nicw_%d', K);
tempvar = struct(fn, Vb1nicw{K});
fnm = [fn '.mat'];
save(fnm, '-struct', 'tempvar');
end
This will save to files Vb1nicw_1.mat through Vb1nicw_10.mat, using a variable that has the same name as the file name.
You have not been clear as to what filenames you want to use or what variable names you want to use within those files.
Zeab
Zeab 2019년 1월 9일
works perfectly,you solved my trouble!
Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by