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!
Accessing and synchronizing timetables in cell array?
15 ビュー (過去 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 件)
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 Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!