Accessing and synchronizing timetables in cell array?

19 次查看(过去 30 天)
I've been working on a script to examine salinity around specific time ranges around high tide (e.g. +/- 30 min, 1 hour, 2 hours, etc), and I'm trying to keep it as systematic as possible so that I can import different dates/locations and run it without any changes. Right now I'm at the point where I have a 296x1 cell array (mr2), with a 5x1 timetable in each cell. Essentially, I'm trying to extract the timetables from this cell array to create one combined timetable. Each timetable has the same variable, and the timestamps are chronological going through cells 1:296. I've been attempting this through a for loop, although I think I'm having an issue with the indexing.
%% Convert cell array to timetable
lengthmr = length(mr2).*5;
iterations = zeros(lengthmr,1);
sz = [lengthmr 2];
varTypes = ["datetime","double"];
varNames = ["Timestamp","Sal_psu"];
T = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
for i = 1:length(iterations)
T(i,"Sal_psu") = mr2{i,1}(i,1);
end
The error message I receive is:
Error using ()
Row index exceeds table dimensions.
Error in COMPARE_salinity_model_MRB_v2m (line 46)
T(i,"Sal_psu") = mr2{i,1}(i,1);
So I know the indexing I'm doing is quite off, but I've been trying to troubleshoot for a few days now and thought I'd reach out to the community. I will also attach the variables to run this bit of code.
TIA!
  1 个评论
Mikaela Martiros
Mikaela Martiros 2023-2-6
Also, apologies if this is a bit clunky! I tend to write scripts by working through my logic step by step, so I often have a lot of variables or a lot of steps for fairly simple processes. I usually work these out after getting what I need from the code, but yeah, be gentle!

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2023-2-7
编辑:Stephen23 2023-2-7
Do not use a loop for this! The simple MATLAB approach is to use a comma-separated list:
For example:
S = load('salinity_code_troubleshoot.mat');
mr2 = S.mr2
mr2 = 296×1 cell array
{5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable}
T = vertcat(mr2{:}) % comma-separated list
T = 1480×1 timetable
Timestamp Sal_psu ___________________ _______ 2022-05-12 19:30:00 6.25 2022-05-12 19:45:00 7.09 2022-05-12 20:00:00 7.78 2022-05-12 20:15:00 8.2 2022-05-12 20:30:00 8.37 2022-05-13 08:00:00 6.54 2022-05-13 08:15:00 7.13 2022-05-13 08:30:00 7.82 2022-05-13 08:45:00 8.04 2022-05-13 09:00:00 8.1 2022-05-13 20:30:00 8.63 2022-05-13 20:45:00 9.38 2022-05-13 21:00:00 9.82 2022-05-13 21:15:00 10.08 2022-05-13 21:30:00 10.22 2022-05-14 08:45:00 6.32

更多回答(1 个)

Luca Ferro
Luca Ferro 2023-2-7
编辑:Luca Ferro 2023-2-7
[cMax,~]=size(mr2);
C=mr2{1} %needs the first 5 elements of the table as preallocation
for ii=1:cMax
C=[C ; mr2{ii}]; %vertically concatenate tables
end
C=sortrows(C); %the data in mr2 is not sorted timewise, so if you want it sorted add this line
Hopefully this is what you were looking for.

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by