# How to Use sort to Sort an Array of Custom Class Objects in MATLAB？

59 views (last 30 days)
jia on 16 Jul 2024 at 8:02
Edited: Stephen23 on 16 Jul 2024 at 11:10
I have already overloaded the eq, lt, and gt methods in my class definition, but I am still encountering an issue when trying to sort using the sort function.
error sort
Incorrect number or types of inputs or outputs for function sort.
##### 3 CommentsShow 1 older commentHide 1 older comment
jia on 16 Jul 2024 at 8:23
classdef PointGroupElement
% POINTGROUPELEMENT is an element of a point group
%
% Parameters
% ------
% R : Real space rotation action of the operator. Square matrix with
% size of the number of spatial demension.
% conjugate : boolean (default false)
properties
R;
conjugate;
antisymmetry;
U;
end
methods
function obj = PointGroupElement(R,options)
arguments
R (:,:) double;
options.conjugate (1,1) logical = false;
options.antisymmetry (1,1) logical = false;
options.U (:,:) = [];
end
[obj.R,obj.conjugate,obj.antisymmetry,obj.U] = deal(real(R),options.conjugate,options.antisymmetry,options.U);
end
function eq_g = eq(obj,g)
arguments
obj ;
g ;
end
eq_g = arrayfun(@(x) eq_(x,g),obj);
function eq_AB = eq_(A,B)
R_eq = norm(A.R-B.R)<1e-5;
basic_eq = R_eq && (A.antisymmetry==B.antisymmetry) && (A.conjugate==B.conjugate);
U_eq = norm(A.U-B.U)<1e-5;
eq_AB = U_eq && basic_eq;
end
end
function result = lt(obj,g)
result = arrayfun(@(x) lt_(x,g),obj);
function result_ = lt_(A,B)
if ~isequal([A.conjugate,A.antisymmetry],[B.conjugate,B.antisymmetry])
if A.conjugate<B.conjugate
result_ = true;
elseif (A.antisymmetry < B.antisymmetry) && A.conjugate==B.conjugate
result_ = true;
else
result_ = false;
end
else
result_ = string(char(reshape(A.R,1,[])))<string(char(reshape(B.R,1,[])));
end
end
end
function result = gt(obj,g)
result = ~(eq(obj,g) + lt(obj,g));
end
end
end
R1 = eye(3);
g = PointGroupElement(R1);
R2 = -R1;
g1 = PointGroupElement(R2);
g1<g2 % 0
g1>g2 % 1
g1==g2 % 0
sort([g1,g2])
% Error using sort
% Incorrect number or types of inputs or outputs for function sort.
Stephen23 on 16 Jul 2024 at 11:07
Edited: Stephen23 on 16 Jul 2024 at 11:10
"I have already overloaded the eq, lt, and gt methods in my class definition"
Why? AFAIK SORT does not use them.
Just define a SORT method.

Divyam on 16 Jul 2024 at 11:00
Edited: Divyam on 16 Jul 2024 at 11:02
Hi Jia, ensure that your custom class is defined in a ".m" file with name as the class name. "MyClass.m" has been attached as the reference class for the code below.
To test sorting, the following code was created in a different MATLAB file in the same directory where the class was defined.
% Creating an array of custom class objects
array = [MyClass(5), MyClass(2), MyClass(8), MyClass(3)];
% Extracting the "Value" of the objects in an array using the arrayfun method
valuesExtract = arrayfun(@(obj) obj.Value, array);
% Sorting the values to get the sorted indices
[~, sortedIndices] = sort(valuesExtract);
% Reorder the array based on the sorted indices
sortedArray = array(sortedIndices);
% Displaying the sorted values
for i = 1:length(sortedArray)
fprintf("%d ", sortedArray(i).Value);
end
2 3 5 8
The issue might be caused by not using "arrayfun" to extract the "Value" property which has to be compared and comparing the property directly.
For more information regarding the use of "arrayfun" method refer to the following documentation: https://www.mathworks.com/help/matlab/ref/arrayfun.html

### Categories

Find more on Structures in Help Center and File Exchange

R2024a

### Community Treasure Hunt

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

Start Hunting!