Loading and unloading data files
16 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Walter Roberson
2013 年 5 月 31 日
編集済み: Walter Roberson
2013 年 5 月 31 日
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 件のコメント
Walter Roberson
2013 年 5 月 31 日
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.
その他の回答 (1 件)
per isakson
2013 年 5 月 31 日
"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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!