How to save specific rows of a Cell Array?

Hi,
I have a cell array O which stores the output of an iterative algorithm. After each 1000 iteration, I want to save the last 1000 rows of O in a .mat file and append it to the previously saved O,
O is a 20000x2 cell array. Each cell in the first column consists of a 95x4 cell which again consists of 1x5 cells. The second column is a 1x1 matrix.
In the 1000th iteration, I want to save the first 1000 rows of O in a .mat file.
In the 2000th iteration, I want to save the second 1000 rows of O (1001th row to 2000th row) and append it to the previously saved .mat file.
In the 3000th iteration, I want to save the third 1000 rows of O (2001th row to 3000th row) and append it to the same .mat file.
and so on......
I use the following code:
if rem(it,1000) == 0
save('O.mat',O(it-999:it), '-append');
end
and I receive the following error:
Can someone please help me with this?
Error using save
Must be a string scalar or character vector.
Error in HMainCode (line 90)
save('O.mat',O(it-999:it), '-append');

 Accepted Answer

Cris LaPierre
Cris LaPierre on 8 Jan 2021
Edited: Cris LaPierre on 8 Jan 2021
When using save, the second input must be a variable name, not data. The variable name is text (string scalar or character vector.
Here is a description of what -append does from the save documentation page.
save(filename,variables,'-append') adds new variables to an existing file. If a variable already exists in a MAT-file, then save overwrites it with the value in the workspace.
For ASCII files, '-append' adds data to the end of the file.
Perhaps this example on the matfile documentation page is what you want?

6 Comments

Sherwin
Sherwin on 8 Jan 2021
Edited: Sherwin on 8 Jan 2021
So what I want to do is impossible? Is not there any other solution to do this?
Perhaps you have not read my full response? You can't do it the way you have implemented, but the linked example should allow you to do what you want.
Sorry, I had missed the last line of you reply. Thanks for the reference; however, I was not able to figure out what I should exactly do. Would you please explain it a little further? (Sorry, I am new to coding). Also, the matFile created like this does not seem to really save anything. My main reason for saving this big cell array is in case electricity goes off since the algorithm takes more than 10 days to run.
You still have to create the mat file using the save command. Once it has been created, you can use the matfile function to modify a variable in the mat file.
This example worked for me.
C=1:100;
save C.mat C
m = matfile('C.mat','Writable',true);
for a=2:5
C(((a-1)*100+1):100*a) = (C(end)+1):100*a;
m.C= [m.C C(end-99:end)];
end
% View last 10 values
z = m.C;
z(end-10:end)
ans = 1×11
490 491 492 493 494 495 496 497 498 499 500
Thank you so much for your help.

Sign in to comment.

More Answers (0)

Asked:

on 8 Jan 2021

Commented:

on 9 Jan 2021

Community Treasure Hunt

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

Start Hunting!