Loading and unloading data files

25 visualizaciones (últimos 30 días)
Jeff
Jeff el 31 de Mayo de 2013
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de Mayo de 2013
Editada: Walter Roberson el 31 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 31 de Mayo de 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.
Jeff
Jeff el 31 de Mayo de 2013
Thanks, Walter. I've used setappdata() before and found it to be pretty nice. I guess I stopped because it felt like I was doing the same thing and was more complicated (i.e. a few more lines of code) than simply loading the data. I should mention that the data I'm loading is rather small, for the most part. It's typically just initialization for various lists etc.
Thanks for your help.

Iniciar sesión para comentar.

Más respuestas (1)

per isakson
per isakson el 31 de Mayo de 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?

Community Treasure Hunt

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

Start Hunting!

Translated by