필터 지우기
필터 지우기

string cell array inside a graph

조회 수: 1 (최근 30일)
Rub Ron
Rub Ron 2020년 9월 1일
댓글: Bruno Luong 2020년 9월 2일
Say I have this:
C = {'a';'b';'c';'d';'e'};
s = [1 1 2 3 1];
t = [2 2 3 4 4];
G = graph(s,t);
G.Edges.Label = C;
With the following two commands I would expect to get the same output, but I get different, why with the second comand only one output is given?
C{[2 3]}
ans =
'b'
ans =
'c'
G.Edges.Label{[2 3]}
ans =
'b'
  댓글 수: 2
Rub Ron
Rub Ron 2020년 9월 1일
with subref is clear because it is a function. I would not think of G.Edges.Label as a function till now. Looks strange.

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

채택된 답변

Bruno Luong
Bruno Luong 2020년 9월 1일
편집: Bruno Luong 2020년 9월 1일
I'm surprise no one give you get an answer. There is some subttle internal working needed to be explained.
The syntax
C{[2 3]}
returns a comma list. No function is called.
The second command
G.Edges.Label{[2 3]}
call a graph method (probably an overloaded subsref) and it returns on the LHS multiple output IF LHS it's provided. If not the default LHS and Matlab ANS (single "variable").
However if users provide 2 LHS they'll get them.
>> [bb,cc] = G.Edges.Label{[2 3]}
bb =
'b'
cc =
'c'
>>
This behavior is very similar to any multiple-output function such as
>> [a,b]=meshgrid(1:3,1:2) % Get 2 LHSs
a =
1 2 3
1 2 3
b =
1 1 1
2 2 2
>> meshgrid(1:3,1:2) % get only the first LHS
ans =
1 2 3
1 2 3
To digest all that think about the following result
>> G.Edges.Label([2 3]) % parenthesis
ans =
2×1 cell array
{'b'}
{'c'}
>> ans{:}
ans =
'b'
ans =
'c'
>>
  댓글 수: 2
Rub Ron
Rub Ron 2020년 9월 1일
it is a surprise that G.Edges.Label in G.Edges.Label{[2 3]} is treated as a function. It loos like a struct variable
Bruno Luong
Bruno Luong 2020년 9월 2일
Expression like this is called by the overloaded subsref function of GRAPH classdef.
However more confusing is this, if you do this, it will return properly
>> E=G.Edges
E =
5×2 table
EndNodes Label
________ _____
1 2 {'a'}
1 2 {'b'}
1 4 {'c'}
2 3 {'d'}
3 4 {'e'}
>> E.Label{[1 3]}
ans =
'a'
ans =
'c'
The first command call graph subsref to return a TABLE
Then the second command call a table subrefs and it return correctly a comma list. The question is how table manages to do that is a little mysterious to me. It might have more sophisticated SUBREFS and might be an intermediate true CELL variable is created before the final result.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Directed Graphs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by