Why does digraph/addnode convert logicals to double?

조회 수: 6 (최근 30일)
Stefan H
Stefan H 2021년 2월 15일
편집: Samayochita 2025년 8월 11일
Hello everybody,
I just encountered a problem using the digraph method "addnode".
It seems to convert variables of type "logical" to "double". Here's a shortened section of the code I'm using.
% define empty node:
eNode = cell2table( {'', false} );
eNode.Properties.VariableNames = {'Name', 'isSelected'} ;
% create variable with graph object:
newGraph.graph = digraph();
% create a node:
newNode = eNode;
newNode.Name = {'firstNode'};
% check class:
class( newNode.isSelected )
ans =
logical
% add node to graph object:
newGraph.graph = addnode( newGraph.graph, newNode );
% check class again:
class( newGraph.graph.Nodes.isSelected )
ans =
double
Any help or explanation for this behaviour would be very much apreciated. The problem with the conversion is that I can't use the "isSelected" property for indexing once it is converted to double.

답변 (1개)

Samayochita
Samayochita 2025년 8월 11일
편집: Samayochita 2025년 8월 11일
Hi Stephan H,
I understand that while using “addnode” method to add nodes to a “digraph”, if the node table includes a variable of type “logical” that variable is automatically converted to “double”.
This happens because when you use “addnode” to add nodes with custom properties, MATLAB stores these properties as variables in the Nodes table of the digraph object. However, the Nodes table only supports certain data types for its variables, and “logical” is not one of them. MATLAB automatically converts unsupported types to “double” as a fallback. This is why “isSelected” property, which is “logical” in your input table gets automatically converted to “double”.
You can still use logical indexing by converting “isSelected” back to “logical” for indexing:
G.Nodes.isSelected = logical(G.Nodes.isSelected);
disp('After converting back:');
disp(class(G.Nodes.isSelected));
You could also update the MATLAB version to R2025a since I observed that this issue does not occur in the latest MATLAB version.
Here are the documentation links for reference:
Hope this was helpful!

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by