Undefined function 'isnan' for input arguments of type 'Nodes2'
3 views (last 30 days)
I have created a class Nodes that creates a node with properties value, next, and previous. Next and previous are set to nan and Nodes 2 takes in an item which is the value.
I have created a seperate handle class that uses Nodes2, it has an obj.head that is set to nan as its property. I have attached some troubleshooting I have below to confirm my filepath is correct.
I am trying to run
which is where the error occurs and where the code stops.
I am not sure what to do and how to move forward.
Thanks for any ideas!
Walter Roberson on 2 Oct 2022
if I understand correctly, obj.head is either a class member or else NaN for the case where the queue is empty.
In the case where obj.head is indeed NaN because the queue is empty then isnan applied to it would be fine because isnan applied to a number is fine.
But consider the case where the queue is not empty so that the head is a class object. Then isnan is applied to the class object and that fails because the class does not define an isnan method.
Is there a particular reason you are representing empty-queue as nan and not as  or an empty object of the class?
More Answers (1)
J. Alex Lee on 5 Oct 2022
For reference, a related question that shows more of the actual classes in question: https://www.mathworks.com/matlabcentral/answers/1817710-inserted-value-failing-to-show-up-in-list?s_tid=mlc_ans_men_view&mentions=true#comment_2398560
I second @Walter Roberson's question about if you can use an empty object:
classdef DLL < handle
head; % head node of the DLL
function obj = DLL()
obj.head = Nodes2.empty();
And in Nodes2
value % the value that will be stored in our DLL
previous % pointer to previous node
next % pointer to next node
function obj = Nodes2(item)
obj.value = item;
obj.next = Nodes2.empty;
obj.previous = Nodes2.empty;
So you can test with the vanilla "isempty"?