MATLAB: Assigning arrays to edge properties of a graph

edgegraphMATLAB

I'm trying to assign arrays to edge properties/weights
tail = 1:3;
head = 2:4;
G = graph(tail,head);
G.Edges.property(1) = 1 % works
G.Edges.property(1) = [1 2] % doesn't work
However , I get the following error while trying to assign an array to a single edge
Unable to perform assignment because the left and right sides have a different number of elements.graph
Is there an alternate way to store array for each edge?

Best Answer

A two-element vector like [1 2] doesn't fit into one element of an array like G.Edges.property.
If you were assigning the vector to the edge property first, this would work:
>> G = graph(tail,head);
>> G.Edges.property(1, :) = [1 2]
>> G.Edges % Check

But if you'd already assigned a scalar to the first element of property that won't work.
>> G = graph(tail,head);
>> G.Edges.property(1) = 1;
>> G.Edges.property(1, :) = [1 2];
Error using graph/subsasgn (line 52)
Unable to perform assignment because the size of the left side is 1-by-1
and the size of the right side is 1-by-2.
I manually line broke the error message to avoid scrolling.
In this case, you need to explicitly state that you want to assign into a piece of property that has two columns.
>> G.Edges.property(1, 1:2) = [1 2];
>> G.Edges % Check