Load .mat from file and assign new name

I have a image.mat file and inside it is positive_sample variable of 4D matrix. I want to load this variable into a new variable. But if I type
new = load('image.mat');
Then new becomes a 1x1 struct and if i want to take positive_sample i must type
new.positive_sample
What I want is to not have to type .positive_sample. Means new = positive_sample after I load image.mat. How can i do this ?

4 Comments

Stephen23
Stephen23 on 24 Jun 2016
Edited: Stephen23 on 24 Jun 2016
@RuiQi: why do you not want to type .positive_sample ?
It just takes one line, and it is fast and robust. Any other solution is going to be much worse than what you are doing now.
Actually loading the data into the structure new is the best way to load the data. Simply loading into the workspace (i.e. without any output variable) is not recommended. So what you are doing it really fine, and there is no obvious reason why you would want to change it: you already have good code!
Hi, I'm not that experienced so I'm sure there are good reasons to prefer this way. But I actually have the same problem and your way is not practicable in my case:
I am using a for loop and in every loop I'm loading a different file (I have hundreds of files). When loading this file, the variable name to adress its data changes in every loop, as it is predetermined by the file itself!
A similar problem occurs if I load a file, but for any reason have no possibility to read out the name of the variable stored in it.
It would be cool to have a possibility to automatically determin the variable name or just to asign it to another given variable. Maybe someone has an idea?
Hoi, "It would be cool to have a possibility to automatically determin the variable name or just to asign it to another given variable"
This is very easy to do. Please start your own question rather than reviving a two year old one.
Thanks very much.

Sign in to comment.

Answers (2)

Try using importdata instead of load
new = importdata('image.mat');
You will be able to call your data by typing only new instead of new.positive_sample.
KSSV
KSSV on 24 Jun 2016
Edited: KSSV on 24 Jun 2016
load('image.mat');
new = positive_sample ;
Or if you don't want to type the line new = positive_sample. Do the above step, and save the variable new into mat file. Next time, when you load you will get new directly.

1 Comment

Try doing that with a MAT-file that contains a variable named 'alpha'.
load('image.mat');
new = alpha;
This is commonly referred to as "poofing" variables into the workspace and can cause unexpected issues.
Inside a function I recommend ALWAYS calling load with an output argument. If you need to refer to one of the variables from the MAT-file repeatedly, define a new variable by performing dot subscripting on that output.
mydata = load('image.mat');
new = mydata.positive_sample;
If you don't know the name of the variable, use dynamic field names.

Sign in to comment.

Tags

Asked:

on 24 Jun 2016

Answered:

on 16 Mar 2022

Community Treasure Hunt

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

Start Hunting!