accessing data from a struct

319 Ansichten (letzte 30 Tage)
cgo
cgo am 21 Jul. 2018
Beantwortet: Anas Alakhras am 28 Feb. 2023
I have R, which is a 1x1 struct, which contains a matrix named X. I want to access X, how do I this? I want to load X into the workgroup.
R(1) doesn't seem to do it.
  2 Kommentare
madhan ravi
madhan ravi am 21 Jul. 2018
Could you upload the code?
Stephen23
Stephen23 am 21 Jul. 2018
How to access data in structures is explained in the MATLAB documentation:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephan
Stephan am 21 Jul. 2018
Bearbeitet: Stephan am 21 Jul. 2018
Hi,
A = R.X
assigns the Matrix saved in the field X of the struct R to A.
% define empty struct
R = struct()
% add field X containing a matrix
R.X = [1 2 3; 4 5 6]
% assign the matrix from field X to new variable A
A = R.X
% get one element from field x and assign to a
a = R.X(2,3)
Best regards
Stephan
  2 Kommentare
Samuel Suakye
Samuel Suakye am 13 Dez. 2022
How then do you open a .mat file which is a struct?
Stephen23
Stephen23 am 13 Dez. 2022
Bearbeitet: Stephen23 am 13 Dez. 2022
"How then do you open a .mat file which is a struct?"
A MAT-file is not a structure, a MAT-file is a container which can hold many variables of many classes. It is certainly possible that one or more of those variables are structures (but that does not mean that the file itself "is" a structure).
MAT-files are never/rarely opened, they are simply LOADed:
S = load(nameofthematfile)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Anas Alakhras
Anas Alakhras am 28 Feb. 2023
R.X

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