load function sometimes alters structure name in the Workspace

I am encountering a weird phenomena where using the load('filename') function to load a structure, alters the structure name in the workspace. This does not occur everytime, but I observed it on multiple occasions:
Example:
close all
clear variables
load('TM24_w')
results in a structure in my workspace named: "TM242_w"
It does load in the correct structure with the correct fields and values but the workspace name is wrong.
Has anyone ever encountered this and is there a solution?

5 Comments

"...but the workspace name is wrong."
Nothing in your question indicates any problem: the names of the variable/s that are loaded do NOT depend in any way on the filename. So the fact that your example file is called "TM24_w.mat" is unrelated to the loaded variable name/s.
Loading directly into the workspace is liable to bugs. It is much more robust to always load into an output variable.
"Nothing in your question indicates any problem:..."
I find it perplexing that when I load in 4 different structure arrays, 3 of them get the exact same name as the filename that I stored them under, and the 4th one get's a random "2" added to the variable name. I did consider that a problem.
"I find it perplexing that when I load in 4 different structure arrays, 3 of them get the exact same name as the filename that I stored them under, and the 4th one get's a random "2" added to the variable name."
Check the name/s of the variable/s stored in the .mat file (e.g using whos). They certainly will not change when they are loaded.
The name of the file is irrelevant.
Again it's the name of the structure that changed. The variables inside the structure are fine.
"Again it's the name of the structure that changed. The variables inside the structure are fine. "
Very unusual. Can you please upload the .mat file by clicking the paperclip button.
What OS are you using?

Sign in to comment.

 Accepted Answer

When you load a mat file without output on the load command, the variable names will be the same as the names inside the mat and not what the file name happens to be.

3 Comments

I do not understand what you are saying. What do you mean by the "names inside the mat"?
"What do you mean by the "names inside the mat"?"
.mat files store one or more variables (or fields of a structure). Variables have names.
The name of the file is irrelevant.
Note that the MATLAB documentation recommends loading into an output variable.
loading directly into an output variable is a good solution, thank you

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!