Load .mat from file and assign new name

63 Ansichten (letzte 30 Tage)
RuiQi
RuiQi am 24 Jun. 2016
Beantwortet: Gabriele Pisetta am 16 Mär. 2022
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 Kommentare
Guillaume
Guillaume am 16 Dez. 2018
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.
hoi
hoi am 16 Dez. 2018
Thanks very much.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Gabriele Pisetta
Gabriele Pisetta am 16 Mär. 2022
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 am 24 Jun. 2016
Bearbeitet: KSSV am 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 Kommentar
Steven Lord
Steven Lord am 24 Jun. 2016
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by