How to save and append variable into a matlab file?

5 views (last 30 days)
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
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

Accepted Answer

Chunru on 20 Jul 2022
Edited: Chunru on 20 Jul 2022
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);
for k=1:5
i = randn(1,1);
matobj.i = [matobj.i 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

More Answers (0)


Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!