How to save and append variable into a matlab file?

조회 수: 28 (최근 30일)
MP
MP 2022년 7월 20일
댓글: MP 2022년 7월 20일
I would like to save variable "i" in a mat file "paxt.mat". This is to be saved in the current directory.
The varaible "i" varies with each loop hence need to append it.
I tried :
save(fullfile(pwd,'paxt4.mat'),'i','-append'); % OR
save('paxt4.mat','i','-append');
Error using save
Unable to write file D:\..\paxt.mat: No such file or directory.
Both of these does not work. I do not undestand what is wrong.
Any help will be greatly appriciated.
Thanks in advance

채택된 답변

Chunru
Chunru 2022년 7월 20일
편집: Chunru 2022년 7월 20일
You can use mat-file objuect. doc matfile for mor details.
% work with the .mat file using matfile object
matobj = matfile('paxt4.mat', 'Writable', true);
matobj.i=[];
for k=1:5
i = randn(1,1);
matobj.i = [matobj.i i];
end
matobj.i
ans = 1×5
-0.2966 -0.4702 -1.5783 -0.8970 -0.9910
whos('-file', 'paxt4.mat')
Name Size Bytes Class Attributes i 1x5 40 double
  댓글 수: 3
Chunru
Chunru 2022년 7월 20일
See the update.
MP
MP 2022년 7월 20일
Yes, that magic worked!
Thank you so much @Chunru.

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

추가 답변 (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