Effacer les filtres
Effacer les filtres

Is their any way to load only selected fields from a saved structure array.

61 vues (au cours des 30 derniers jours)
Bonnie Tyler
Bonnie Tyler le 15 Fév 2017
Commenté : Walter Roberson le 15 Fév 2017
I have some very large structure arrays stored in .mat files. I want to avoid loading the entire array to save time and memory.

Réponses (2)

Stephen23
Stephen23 le 15 Fév 2017
Modifié(e) : Stephen23 le 15 Fév 2017
Method
Make sure you save the structure array using the '-struct' option:
save(filename,'-struct','S')
And then, exactly as the load documentation says, you can load the variables by supplying their names as optional arguments. E.g. if the mat file contains variables X and Y:
S = load(filename,'X','Y');
This will only load X and Y into the structure S, not matter what other variables are also present in the mat file.
Example
>> S.X = 1:3;
>> S.Y = 4:5;
>> S.Z = 6:9;
>> save('test.mat','-struct','S')
>> clear
>> S = load('test.mat','X','Y');
>> fieldnames(S)
ans =
'X'
'Y'
>> S.Y
ans =
4 5
  1 commentaire
Walter Roberson
Walter Roberson le 15 Fév 2017
However, if they are structure arrays rather than scalar structures, then you cannot use save -struct

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 15 Fév 2017
Sorry there is no Mathworks provided way to do that.
If you just happen to be using a v7.3 mat file then I wonder if there would be an approach by using hdf5 routines to access the mat file?

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by