How to extract cell array of 2D matrices into a timeseries?

조회 수: 4 (최근 30일)
Daniel Flisek
Daniel Flisek 2025년 1월 10일
댓글: Daniel Flisek 2025년 1월 13일
I have some data that is stored as a 1666x1 cell array. Each cell contains a 4x19 double. I need to extract this data and attach it to a timeseries object. Timeseries will not take a cell array directly, so I need to extract the cells.
So let's say the cell array is named data, and the timeseries is ts. I need:
ts.Data(1) = data{1}
ts.Data(2) = data{2}
etc.
cell2mat() concatenates everything into 6664x19, and I can't get reshape() to chop it up into the correct 4x19 blocks. I could do it in a for loop, but that's not very satisfying in MATLAB.
I know this is a simple syntax thing, but I can't seem to find an answer that works.
  댓글 수: 1
Burak
Burak 2025년 1월 10일
편집: Burak 2025년 1월 10일

To populate a timeseries object with data from a cell array where each cell contains a 4x19 double, you can indeed use a for loop. However, if you want a more MATLAB-esque solution (avoiding explicit loops), you can use arrayfun to achieve this. Here’s how you can do it:

   % Initialize the timeseries object with an empty array of the correct size
    ts = timeseries();
    % Use arrayfun to attach data to the timeseries object
    ts.Data = cat(3, data{:});

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

채택된 답변

Walter Roberson
Walter Roberson 2025년 1월 10일
data = {rand(2,5); rand(2,5); rand(2,5)};
ts = timeseries(cat(3,data{:}))
timeseries Common Properties: Name: 'unnamed' Time: [3x1 double] TimeInfo: tsdata.timemetadata Data: [2x5x3 double] DataInfo: tsdata.datametadata
getsamples(ts,1)
timeseries Common Properties: Name: 'unnamed' Time: [1x1 double] TimeInfo: tsdata.timemetadata Data: [2x5 double] DataInfo: tsdata.datametadata
So in general you
ts = timeseries(cat(3, data{:}));
  댓글 수: 1
Daniel Flisek
Daniel Flisek 2025년 1월 13일
That worked! I knew it'd be something simple. Thanks for taking the time to answer.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Collections에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by