フィルターのクリア

how to append a mat file

77 ビュー (過去 30 日間)
nas illmatic
nas illmatic 2019 年 3 月 17 日
コメント済み: Maxwell Rosen 2021 年 8 月 2 日
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
Walter Roberson 2019 年 3 月 17 日
You cannot do that with save()

nas illmatic
nas illmatic 2019 年 3 月 17 日
There should be a way to read back the mat file, then update it to keep the existing column, and write a new column, with the data to plot and it's label, then save it back to the matfile.
  1 件のコメント
Walter Roberson
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
nas illmatic 2019 年 3 月 17 日
編集済み: nas illmatic 2019 年 3 月 17 日
I would like to use built-in functions whenever possible. Will this work?
% mydata.mat only has a 'variable' -
% it is a 2 rows, 1 column cell by cell named mydatacell
% which is row 1 data, and row 2 the label name
%Define Matfile
m = matfile(mydata.mat,'Writable',isWritable);
%Find Size
[nrows,ncols] = size(m,'mydata');
%Append the data
m.mydatacell[m.mydatacell ; ncols+1] = [1.2 ; 'mylabel'] % I think it's semicolon here to go to column
save(mydata.mat,mydatacell,'-append');
  4 件のコメント
nas illmatic
nas illmatic 2019 年 3 月 17 日
I created a simple example and utilized the 'matfile' that you pointed to me, but for some reason, my 'mydatacell' which I expected it to grow from a 2x1 cell to a 2x2 cell (where the second column contains 1.2 and 'mynewlabel') but for some reason it's not growing....
mydatacell= {1.1; 'myoldlabel'};
save('mymat.mat','mydatacell')
m = matfile('mymat.mat','Writable',true);
newdata = {1.2; 'mynewlabel'};
if ismember('mymat', fieldnames(m)) %is the variable already present?
[nrows, ncols] = size(m, 'mydatacell');
m.mydatacell(:,ncols+1) = newdata; %yes, extend it
else
m.mydatacell = newdata; %no, assign it
end
save('mymat.mat','mydatacell')
Maxwell Rosen
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWorkspace 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