Question

I have a struct (of matrices) in Matlab that has been saved on the harddisk. I currently use load to load these files inside my functions. Do you have any suggestions of doing this someother way that is much faster?

(Yes, I can pass the struct as a variable to my function but that is not possible due to memory issues!). Thanks! This would be a great help!

A = struct('local', randn(200000,14), 'usd', randn(200000,14), ...
          'ttm', randn(180000,14), 'avg', randn(190000,14), ...
          'ttm1yr', randn(190000,14), 'avg1yr', randn(190000,14)) ;
save('A.mat', 'A') ;                     clear all;clc

tic, load A.mat, A=A.local; toc  %--> Takes 1.05 seconds
Was it helpful?

Solution

It appears that you're interested only in specific chunks of your saved file. I would suggest changing the format of the saved data so that individual variables can be loaded using the

S = load(filename, variables)

form of load. This will speed up the loading significantly, since you'll avoid copying all of the unwanted data from disk into memory, just to free it right away.

If your data is already in struct form, you can use this form of save (from the online docs):

save(filename, '-struct', structName, fieldNames) stores the fields of the specified scalar structure as individual variables in the file. If you include the optional fieldNames, the save function stores only the specified fields of the structure. You cannot specify variables and the '-struct' keyword in the same call to save.

OTHER TIPS

Starting in R2011a you can access the contents of a Mat file without using load via the matfile object. help matfile for details.

The real advantage is that this allows read/write of portions of large arrays without loading or saving the entire array.

This is of limited use to you with your current structure, since fields of structures cannot be indexed this way. But a relatively small re-factor would allow you to take advantage of these features, depending on what the rest of your application looks like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top