Saving workspace variables with different .mat file name using for loop
이전 댓글 표시
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
Rik
2019년 1월 7일
If you want to give the mat files each its own name, you can use sprintf to generate a name from a pattern.
Stephen23
2019년 1월 7일
"...and therfore the diffrent variables should be named diffrently,"
That would not be a good approach. Read this to know why:
It is really much easier to process a sequence of .mat files if the variable names are exactly the same for each .mat file, and only the filename changes.
Zeab
2019년 1월 9일
@Zeab: look at your code:
for i=1:10
temp=x{i,1}.data;
fname=sprintf('myfield%d.mat',i);
save(filename)
end
On the third line you define fname... which you never use. And on the fourth line you try to use filename, which you never define. Have another look at my answer: did I use the same variable fnm on both lines three and four? Did you?
Zeab
2019년 1월 9일
Stephen23
2019년 1월 9일
"...it is desired to save the 1D data field only with 10 diffrent .mat file name."
You can use my answer. Just extract the required data into the variable temp and my code will generate a new filename on each loop iteration.
Zeab
2019년 1월 9일
Zeab
2019년 1월 9일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 String에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!