MATLAB: Sort depends on value with cellfun

cellfunMATLABsort

Hello,
I would like to sort my JSON data order by "createdAt" column.
But it gives an error "Index exceeds the number of array elements (1)."
data = loadjson('C:/data/default.json');
[~,X] = sort(cellfun(@(cellElem) cellElem.createdAt, data.Location,'UniformOutput',false));
data = data(X);
count_data = sum(cellfun(@(x) numel(x), data.Location));

Best Answer

I've tried like this and it's working now.
x = cellfun( @(cellElem) cellElem.locationX, data.Location );
y = cellfun( @(cellElem) cellElem.locationY, data.Location );
% Get date strings
d = cellfun( @(cellElem) cellElem.createdAt, data.Location, 'UniformOutput', false)
% Convert to datetime
d = datetime( d, 'InputFormat', 'dd-MM-yyyy HH:mm:ss' );
% Get the sort order
[~,idx] = sort( d );
% Sort other arrays
x = x(idx);
y = y(idx);