How can I append the same variables on mat file?

Hi
I need help on how to update the same variables of an exisitng mat file and alhough there are couple threads online on the subject, I still haven't found solution to the problem.
I run different instantiations of my script, which always loads the same mat file and I need my variables (of the different instantiations) to be saved in the mat file in a new row. In other words with every instantioantion of my scirpt I need a new row to me added in my mat file. How do I do this seemingly simple task? Thanks.
filename=(['my_file.mat']);
m = matfile(filename,'Writable',isWritable)
save(filename,'m.my_variables','-append');

6 Comments

It sounds like you want to append data to your variable, not your mat file. That means you'll have to load the variable, append the data, and store the variable back into the mat file.
No, you do not need the load/append/store if you use matfile.
Fran
Fran on 3 May 2019
Edited: Fran on 3 May 2019
Thanks Rik. I tried your idea and it doesn't seem to work so far. Can you offer an example on how to use load/append/store to append the variable of the mat file with a new row of data?
Thanks
I am sorry for bad post.
I would like to ask, if there is some similiar way to save even multidimensional matrixes? I need them to be saved for future analysis but I can't find a single way. All of them are in the same variable. Thank you
y2 and y3 are multidimensional matrixes like 5D, 5x5 and I would need atleast one of them (both have same parametres). For "normal variables" I don't use that row s = size(m1, 'bp_MatrixesErrors'); and instead of that I use my own counter and it works great. But with that s = size.. I get error "Function 'subsindex' is not defined for values of class 'matlab.io.MatFile'." and I don't know, how to make it with another way.
mater=('bp_MatrixesErrors.mat');
m1 = matfile(mater,'Writable',true);
new_row_of_values = [y2, y3];
if isprop(m1, 'bp_MatrixesErrors')
s = size(m1, 'bp_MatrixesErrors');
m1.bp_MatrixesErrors(s(1)+1, :) = new_row_of_values;
else
m1.bp_MatrixesErrors = new_row_of_values;
end
Maybe it is best if you post this as a separate question with enough example data that we can run your code. Feel free to post a comment here with the link.

Sign in to comment.

 Accepted Answer

After you have created m = matfile() then
new_row_of_values = something appropriate;
s = size(m, 'my_variables');
m.my_variables(s+1, :) = new_row_of_values;
Do not just use
m.my_variables(end+1, :) = new_row_of_values; %avoid this
as apparently using "end" forces the entire variable to be loaded into memory.

5 Comments

thank you Walter
I followed your instructions:
filename=('my_file.mat');
m = matfile(filename) %,'Writable',isWritable); %the part "isWritable" gives error
"undefenied function or variable", so I commented this out for now.
new_row_of_values = 'normal_theta, max_theta_pwr, max_theta_freq, max_pwr, max_freq';
s = size(m, 'my_variables');
m.my_variables(s+1, :) = new_row_of_values;
here is the error that I get:
'my_variables' does not exist in '/home/......my_directory...../my_file.mat'.
thanks for any further comment on this,
Alexandra
If the variable does not exist yet in the file you could get that problem. For that possibility, you could check
if isfield(m, 'my_variables')
s = size(m, 'my_variables');
m.my_variables(s(1)+1, :) = new_row_of_values;
else
m.my_variables = new_row_of_values;
end
again thanks a lot! I can create the file and write in it. Only issue is that my.variables it is not being appended to a new line, but the first line is rewritten in every instantiation of my script.
filename=('my_variables.mat');
m = matfile(filename,'Writable',true);
new_row_of_values = [normal_theta, max_theta_pwr, max_theta_freq, max_pwr, max_freq];
if isfield(m, 'my_variables')
s = size(m, 'my_variables');
m.my_variables(s(1)+1, :) = new_row_of_values;
else
m.my_variables = new_row_of_values;
end
Change the isfield() to isprop()

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!