Is {} used as index array in class?

조회 수: 5 (최근 30일)
Thang Nguyen
Thang Nguyen 2013년 1월 16일
Hi, While I am studying Matlab object oriented I found this example and I want to confirm it.
I have a constructor:
function obj = DocPortfolio(name,varargin)
if nargin > 0
obj.name = name;
for k = 1:length(varargin)
obj.indAssets{k} = varargin(k);
assetValue = obj.indAssets{k}{1}.currentValue;
obj.totalValue = obj.totalValue + assetValue;
end
end
end
indAssets is an array property. Is it true that obj.indAssets{k} is a way to index into this array? Cause I have never use {} to index array before. And what is obj.indAssets{k}{1} in next line?
Thanks!

채택된 답변

Cedric
Cedric 2013년 1월 16일
편집: Cedric 2013년 1월 16일
Your question is not related to OOP. Look at the following:
>> c = {5, {'Hello', 'World'}, struct( 'a', 8, 'b', 9 )} ;
c = [5] {1x2 cell} [1x1 struct]
Here, c is a cell array (an array of cells).
>> class( c )
ans = cell
You can index blocks of this cell array using ()-type indexing.
>> c(1:2)
ans = [5] {1x2 cell}
>> c(1)
ans = [5]
The latter is the first element of the cell array c, which is a cell in itself.
>> class( c(1) )
ans = cell
For extracting the content of this cell, you have to use {}-type indexing.
>> c{1}
ans = 5
This time it (the content) is the double 5.
>> class( c{1} )
ans = double
Now indexing can be "nested"; c{2} is the content of cell #2 of the cell array c. This content is a cell array in itself, that you can again index with both () and {} whether you want cells or their content.
>> c{2}
ans = 'Hello' 'World'
>> class( c{2}(1) )
ans = cell
>> class( c{2}{1} )
ans = char
Or when the content is a struct..
>> class( c{3} )
ans = struct
>> c{3}.a
ans = 8
Hope it helps,
Cedric
  댓글 수: 2
Thang Nguyen
Thang Nguyen 2013년 1월 16일
Thanks Cedric for spending time to answer my question. I highly appreciate your help!
Cedric
Cedric 2013년 1월 16일
You are most welcome! Please accept the Answer if it answers your question.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by