Accessing and synchronizing timetables in cell array?
Mostrar comentarios más antiguos
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 comentario
Mikaela Martiros
el 6 de Feb. de 2023
Respuesta aceptada
Más respuestas (1)
Luca Ferro
el 7 de Feb. de 2023
Editada: Luca Ferro
el 7 de Feb. de 2023
0 votos
[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.
1 comentario
Mikaela Martiros
el 7 de Feb. de 2023
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!