how to append a mat file
이전 댓글 표시
Hi - I am using the save command to save a variable in my workplace which is two rows and one column worth of data (2 by 1). However, I would like the 'append' command to move one column the right evertime it is run (as not to replace my first column worth of data), however reading the 'save' help file, it only 'appends' if it is ASCI but does not 'append' when it is a .mat file. How do I prevent my data from being overwritten and instead having new data keep being added to the right (this way, I can just load it, and plot all of first row and use the second row as 'labels' (for the legend))...
save(data.mat,datacell,'-append')
답변 (3개)
Walter Roberson
2019년 3월 17일
0 개 추천
You cannot do that with save()
Have a look at https://www.mathworks.com/help/matlab/ref/matfile.html
nas illmatic
2019년 3월 17일
0 개 추천
댓글 수: 1
Walter Roberson
2019년 3월 17일
Sure. just load the appropriate variable from the mat file , make the change and save with append (which does work for mat files by replacing the specified variables and leaving the rest in place . ) The built in functionality to handle that is matfile() but you can write your own function .
nas illmatic
2019년 3월 17일
편집: nas illmatic
2019년 3월 17일
댓글 수: 4
Walter Roberson
2019년 3월 17일
You can click on "Comment on this Answer" to reply to someone, instead of creating a new Answer each time.
Walter Roberson
2019년 3월 17일
newdata = {1.2; 'mylabel'};
if ismember('mydata', fieldnames(m)) %is the variable already present?
[nrows, ncols] = size(m, 'mydatacell');
m.mydata(:,ncols+1) = newdata; %yes, extend it
else
m.mydata = newdata; %no, assign it
end
save is not needed.
At the moment I do not see any nice way of checking whether a variable exists in a matfile, so I use the work-around of looking for the variable name in the fieldnames . fieldnames will always exist even if the file has not been written to yet, and will always contain the field 'Properties' at least.
nas illmatic
2019년 3월 17일
Maxwell Rosen
2021년 8월 2일
I think you have to delete that last save() function. Matlab automatically appends your array that you opened m. See the function https://www.mathworks.com/help/matlab/ref/matlab.io.matfile.html in the example Load and Save Parts of Variables
카테고리
도움말 센터 및 File 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!