Why does digraph/addnode convert logicals to double?
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (1)
Samayochita
le 11 Août 2025
Modifié(e) : Samayochita
le 11 Août 2025
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!
0 commentaires
Voir également
Catégories
En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!