Is there a way to access a struct object's reference (handle) in MATLAB?

19 ビュー (過去 30 日間)
Etienne Lebard
Etienne Lebard 2020 年 12 月 10 日
コメント済み: Paul Hoffrichter 2020 年 12 月 11 日
Let's say I have a struct object A with some fields :
A.a=1;
I have an another struct objetc B with some fields, one of them being the value of A :
B.b=A;
My issue is that when I change fields of A, it won't affect B.b :
> A.a=2
A =
struct with fields:
a: 2
>> B.b
ans =
struct with fields:
a: 1
Is there a "built-in" way to tell MATLAB that I want B.b to be not a copy of value of A, but a reference to A, so that when I change A it reflects when I access B.b?
I mean, besides defining and using classes which inherit from handle?

採用された回答

Paul Hoffrichter
Paul Hoffrichter 2020 年 12 月 11 日
編集済み: Paul Hoffrichter 2020 年 12 月 11 日
This may be close to what you are looking for. Define a classA.m
classdef classA < handle
properties
a
end
methods
function obj = classA(a)
obj.a = a;
end
end
end
Run the following:
>> A = classA(1)
A =
classA with properties:
a: 1
>> A.a
ans =
1
>> B.b = A;
>> B.b
ans =
classA with properties:
a: 1
>> A.a = 2
A =
classA with properties:
a: 2
>> B.b
ans =
classA with properties:
a: 2
>> B.b.a
ans =
2
  2 件のコメント
Etienne Lebard
Etienne Lebard 2020 年 12 月 11 日
Thanks! :)
I thought maybe there was a way to access the handle of a struct object, but that solution does what I need it to do
Paul Hoffrichter
Paul Hoffrichter 2020 年 12 月 11 日
Glad to have helped.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by