Clear Filters
Clear Filters

Matfile modify the variable of a structure

6 views (last 30 days)
Hello all
I need advices on how to use matfile().
I have a .mat file which is a structure. This file is called '2019hydro.mat' and is a structure subdivided into 9 330x300x365 matrices.
When I load the file I have the structure 'global_structure' subdivided into the 9 matrices ('smb', 'albedo', 'abl' etc...)
Everyday there are 9 330x300 matrix that I calculate to iterate in this global_structure depending on the day. Let's say we are the 31st of December, for the variable 'smb' I will write :
global_structure.smb(:, :, 365) = calculated_smb;
Because this structure is really heavy, I was advised to use matfile().
To test my code, I wrote :
m = matfile('2019hydro.mat', 'Writable', true)
And normally to iterate the calculated matrices I would write:
m.global_structure.smb(:, :, 365) = calculated_smb;
But I get an error message saying "Cannot index into 'global_structure' because MatFile objects only support '()' indexing." .
However when I open 'm' in the workspace I have 2 structures: 'properties', and 'global_structure' which has my 9 330x300x365 matrices.
Why can I see the matrices in my 'global_structure' by clicking on it from the workspace, but I can't access it from the command window by typing 'm.global_structure.smb' ? I also tried doing an imagesc to display the data on a specific day (eg: imagesc(m.global_structure.smb(:, :, 364) ) but I have the same error message.
How can I get rid of this issue ?
Thanks for your help !
ps: I can't upload the file because of its size, I hope I was clear enough. In case I wasn't, I will explain my issue again more clearly.

Accepted Answer

Ameer Hamza
Ameer Hamza on 18 May 2020
matfile does not support indexing into the struct fields. Read the limitations here:
You will need to modify the complete struct
m = matfile('2019hydro.mat', 'Writable', true)
temp = m.global_structure;
temp.smb(:, :, 365) = calculated_smb;
m.global_structure = temp;
V.D-C on 19 May 2020
Hopefully it will be implemented in Matlab's next version.
Thank you very much for your answer, it works like a charm !
Michael Shagam
Michael Shagam on 30 Mar 2021
Doesn't this defeat the purpose of the matfile function of not loading whole variables into memory? Executing
temp = m.global_structure
reads the entire structure global_structure into memory

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!