Loading and unloading data files
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
In the applications I'm working on I typically store information in .mat files which I then load several times throughout the application. I don't like passing this data through global variables so I end up loading the data several times. My question is does this data get saved into memory each time? Or does each load remove the previous version in memory? Additionally, do I need to close the file somehow?
Thanks, Jeff
0 commentaires
Réponse acceptée
Walter Roberson
le 31 Mai 2013
Modifié(e) : Walter Roberson
le 31 Mai 2013
Data that is created in a workspace is removed once that function terminates (provided no reference to it is being held somewhere)
However, if you have a structure such as
A loads file and calls B
B calls C
C calls D
D loads file
D returns
C calls E
then the copy of the file that was loaded in D is discarded when D returns, but at the point that D does the load, there are multiple copies of the data (one in A) because A has not yet terminated.
You might want to consider storing the data as app data using setappdata(). Myself I would consider that to be equivalent to using a global variable (though one slightly less likely to be inadvertently changed.)
If you have a gui interface, you could use guidata()... which is setappdata() in disguise... which is at least in theory "local" to a figure() rather than being global, but most people treat it as being global, and about the only time most people deal with the fact that it is local is if they try to merge two GUIDE-created interfaces, and then they are likely to be cursing the fact that it is not completely global.
Anyhow...
3 commentaires
Walter Roberson
le 31 Mai 2013
Right. But if you use one of the alternatives I mention, then there would usually be only a single copy of the actual storage, unless you changed one of the variables.
Plus de réponses (1)
per isakson
le 31 Mai 2013
"does this data get saved into memory each time". I'm not sure I understand correctly. The system cache is affected and data are written to disk.
"Or does each load remove the previous version in memory?". The values of the Matlab variables are replaced each time. If the data are still in system cache that is used otherwise data are read from disk.
No, you don't need (and cannot) close the file explicitly.
Are you aware save( .... , '-v6' ) is faster?
0 commentaires
Voir également
Catégories
En savoir plus sur Whos 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!