Matfile modify the variable of a structure

4 次查看(过去 30 天)
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.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-18
matfile does not support indexing into the struct fields. Read the limitations here: https://www.mathworks.com/help/releases/R2020a/matlab/ref/matlab.io.matfile.html#mw_cd9f9130-9398-4df9-9729-070d19d4c781
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;
  2 个评论
V.D-C
V.D-C 2020-5-19
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 2021-3-30
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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!

Translated by