MATLAB: How to Call the Name of a Loaded File

loadstruct

I load B which is a struct as follows:
A = load( ‘B.mat’ );
How can I later access the name of file B from A? When I call A, I only get the name of all fields, but not the name of the MAT File B

Best Answer

"From A", you can't; the data stored in a .mat file are totally independent of the (root) name given the file when it was created. The only way to create a connection would be to add another variable or field in the structure that contains the filename you're going to use when SAVE the data. Of course, this runs the risk you don't update the field correctly or at all, so it's not a sure-fire method, either.
What's the problem you're actually trying to solve? If you have 'b.mat' to load the file, what's to keep from creating the link from that information to do whatever it is that you wish to do?
But the upshot is, there is nothing within a standard .mat file than the variables written to it; if you don't add information within the file if the filename that was used to load it is somehow lost it can't be told which file it was.
Related Question