필터 지우기
필터 지우기

How do I access a structure which is available inside a method of a class?

조회 수: 1 (최근 30일)
if true
classdef test1
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function example(obj)
...
% The calculation Part
...
stru % The calculation part returns "struct" which is a structure
%with 5 fields and these 5 fields have some sub fields in them
end
end
The above shown code is my class(test1) which has a method named "example" which calculates a structure named "stru". Stru is a structure with 5 fields and those 5 fields in turn have 3 subfields. ----------------------------------------------------------------------------------------------
My Question: How can I access the structure "stru" in some other method of a different class but which is also stored in the same working directory. Please note, I am using both classes in my GUI.

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 8월 8일
Store it as a property of the class:
classdef SomeClass < handle
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
aStruct
end
methods
function method1(obj)
obj.aStruct = struct('hello',1);
end
function method2(obj)
disp(obj.aStruct)
end
end
end
Using it:
x = SomeClass
method1(x)
method2(x)
  댓글 수: 2
matlablearner
matlablearner 2014년 8월 8일
Thanks for your quick response. In your code, If I already have two other properties defined and they are being used in the same method, How can I access this propoerty in specific? Excuse me If my question is very naive. I have Included my doubt in the code below.
if true
% classdef SomeClass < handle
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
aStruct
bInfo
cInfo
end
methods
function method1(obj)
obj.aStruct = struct('hello',1);
obj.bInfo = blabla;
end
function method2(obj)
disp(obj.aStruct)
end
end
So Now how do I access the aStruct in some other class and not in the same class. Thanks a lot.
Sean de Wolski
Sean de Wolski 2014년 8월 8일
That other class' method would have to receive the object as an input
otherclassmethod(other_class_obj, some_class_obj)
some_class_obj.aStruct.b

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by