Undefined function 'isnan' for input arguments of type 'Nodes2'

조회 수: 2 (최근 30일)
Manaal
Manaal 2022년 10월 2일
댓글: Walter Roberson 2022년 10월 6일
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
if (~isnan(obj.head))
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
Walter Roberson 2022년 10월 2일
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?
  댓글 수: 1
Steven Lord
Steven Lord 2022년 10월 3일
You could also define an isnan method for the class that checks if the value property of the object is NaN. You could implement the nan function for your class and use nan('Nodes') to make a "placeholder" object to use as your head and tail nodes.

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

추가 답변 (1개)

J. Alex Lee
J. Alex Lee 2022년 10월 5일
I second @Walter Roberson's question about if you can use an empty object:
classdef DLL < handle
properties
head; % head node of the DLL
end
methods
function obj = DLL()
obj.head = Nodes2.empty();
end
end
end
And in Nodes2
classdef Nodes2
properties
value % the value that will be stored in our DLL
previous % pointer to previous node
next % pointer to next node
end
methods
end
function obj = Nodes2(item)
obj.value = item;
obj.next = Nodes2.empty;
obj.previous = Nodes2.empty;
end
end
So you can test with the vanilla "isempty"?

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by