I have a structure, He, which is a database containing a bunch of helium mass spec data, in addition to the date that it was collected. So each entry into the He structure will have various fields, for example, Date, PanName, ExtractPeriod, Data, etc. Because the entries into the He structure aren't always chronological, I want to be able to rearrange them by the Date field. I have tried exporting the dates into a new variable and sorting that, but MATLAB insists that it can't sort a cell array. I try to convert it from a cell array to a datetime array, then MATLAB insists that it isn't a cell array (it is, class() returns 'cell'). I have tried converting it to a datetime array using cell2mat, but again, it's insisting that it isn't a cell array (again, it is).
>> date_list = {He.Date}
>> sort(date_list)
Error using sort
Input argument must be a cell array of character vectors.
>> date_list = cell2mat(date_list)
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.
>> class(asdf)
ans =
'cell'
What's the easiest way to go about doing this and what am I doing wrong? Why does it tell me it's a cell on one line, then tell me it's not a cell in the next?? All I want to do is sort this database by date, but I can't even get to the point where I can sort a list of the dates...

댓글 수: 3

Rik
Rik 2019년 9월 18일
The sort function is telling you that not all cell elements contain a char vector. The cell2mat function tells you that some element contains a data type that it can't handle.
If you attach your struct in a mat file we might be able to help you generate a sorted list.
>> d = datetime
d =
datetime
19-Sep-2019 00:08:38
>> methods(d)
... sort ...
The value of the date field, what class is it ?
Adam Goldsmith
Adam Goldsmith 2019년 9월 19일
편집: Adam Goldsmith 2019년 9월 19일
The class of the Date field is 'datetime'
Unfortunately, the file is larger than the 5 MB limit and can't be uploaded, however, you could really easily generate a dummy structure with the same issue:
examplestruct = struct;
for i = 1:5
examplestruct(i).data = rand(1,10)
end
examplestruct(1).date = datetime('09/01/2019','InputFormat','MM/dd/yyyy')
examplestruct(2).date = datetime('08/11/2019','InputFormat','MM/dd/yyyy')
examplestruct(3).date = datetime('09/12/2019','InputFormat','MM/dd/yyyy')
examplestruct(4).date = datetime('09/16/2019','InputFormat','MM/dd/yyyy')
examplestruct(5).date = datetime('09/05/2019','InputFormat','MM/dd/yyyy')

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

 채택된 답변

Rik
Rik 2019년 9월 19일

0 개 추천

Don't concatenate to a cell, but to an array:
examplestruct = struct;
for i = 1:5
examplestruct(i).data = rand(1,10);
end
examplestruct(1).date = datetime('09/01/2019','InputFormat','MM/dd/yyyy');
examplestruct(2).date = datetime('08/11/2019','InputFormat','MM/dd/yyyy');
examplestruct(3).date = datetime('09/12/2019','InputFormat','MM/dd/yyyy');
examplestruct(4).date = datetime('09/16/2019','InputFormat','MM/dd/yyyy');
examplestruct(5).date = datetime('09/05/2019','InputFormat','MM/dd/yyyy');
date_list =[examplestruct.date];
[~,order]=sort(date_list);
sorted=examplestruct(order);

댓글 수: 2

Adam Goldsmith
Adam Goldsmith 2019년 9월 19일
This works, thank you! I had seen using {struct.field} to turn the structure field into a cell array, but wasn't aware that I could do [struct.field] to convert it into an array. That's very helpful, thank you!
Stephen23
Stephen23 2019년 9월 19일
"I had seen using {struct.field} to turn the structure field into a cell array, but wasn't aware that I could do [struct.field] to convert it into an array"
Read these to know how it works and where you can use that syntax:

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

추가 답변 (1개)

xi
xi 2019년 9월 19일

0 개 추천

It's easier to handle it in table:
T=struct2table(examplestruct);
sort(T.date)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2019년 9월 18일

댓글:

2019년 9월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by