Clear Filters
Clear Filters

Hello, How can I make the output timetable look similar in the photo, so it will 901*1 instead of 901*3

34 views (last 30 days)
mission.StartDate = datetime(2021,1,1,12,0,0);
missionDuration = 2.5;
mission.Duration = hours(2.5);
startLLA = [38.13425 140.44777 10000];
endLLA = [38.1499994 140.6333308 10000];
timeOfTravel = [0 3600*missionDuration];
sampleRate = 10; % The trajectory is sampled every 10 seconds.
trajectory = geoTrajectory([startLLA;endLLA],timeOfTravel,'SampleRate',sampleRate);
sampleTimes = timeOfTravel(1):sampleRate:timeOfTravel(end)
aircraftLLA = lookupPose(trajectory,sampleTimes)
% Add timestamps to the data and generate a timetable.
t = (0:sampleRate:timeOfTravel(end))';
tsec = seconds(t);
aircraftLLAin = [t aircraftLLA];
aircraftTT = array2timetable(aircraftLLA,"RowTimes",tsec);

Accepted Answer

Star Strider
Star Strider on 25 May 2024 at 15:06
You can do that once, however if you write it and then read it, it will lose the original formatting.
To illustrate —
Time = datetime('now') + seconds(0:10:80).';
Time.Format = 'ss';
Time = Time - Time(1);
LLA = rand(numel(Time),3);
aircraftTT = timetable(LLA, 'SampleRate',0.1)
aircraftTT = 9x1 timetable
Time LLA ______ __________________________________ 0 sec 0.61493 0.34451 0.29506 10 sec 0.16768 0.80452 0.0056361 20 sec 0.24866 0.27376 0.57078 30 sec 0.4567 0.074106 0.98097 40 sec 0.65259 0.83117 0.1005 50 sec 0.91976 0.82536 0.01409 60 sec 0.76724 0.82312 0.97425 70 sec 0.060176 0.31849 0.58381 80 sec 0.35935 0.49803 0.76681
writetimetable(aircraftTT, 'Aircraft.xlsx')
aircraftT2 = readtable('Aircraft.xlsx');
aircraftT2.Time = seconds(cellfun(@str2double, regexp(aircraftT2.Time, '\d*', 'match')));
aircraftTT2 = table2timetable(aircraftT2)
aircraftTT2 = 9x3 timetable
Time LLA_1 LLA_2 LLA_3 ______ ________ ________ _________ 0 sec 0.61493 0.34451 0.29506 10 sec 0.16768 0.80452 0.0056361 20 sec 0.24866 0.27376 0.57078 30 sec 0.4567 0.074106 0.98097 40 sec 0.65259 0.83117 0.1005 50 sec 0.91976 0.82536 0.01409 60 sec 0.76724 0.82312 0.97425 70 sec 0.060176 0.31849 0.58381 80 sec 0.35935 0.49803 0.76681
.

More Answers (1)

Abhishek Kumar Singh
Abhishek Kumar Singh on 25 May 2024 at 15:49
You can modify last couple of lines in following way to get desired, add/modify the last couple of lines to use mergevars to get the 3 columns under one column.
Refer to the modified snippet below:
mission.StartDate = datetime(2021,1,1,12,0,0);
missionDuration = 2.5;
mission.Duration = hours(2.5);
startLLA = [38.13425 140.44777 10000];
endLLA = [38.1499994 140.6333308 10000];
timeOfTravel = [0 3600*missionDuration];
sampleRate = 10; % The trajectory is sampled every 10 seconds.
trajectory = geoTrajectory([startLLA;endLLA],timeOfTravel,'SampleRate',sampleRate);
sampleTimes = timeOfTravel(1):sampleRate:timeOfTravel(end);
aircraftLLA = lookupPose(trajectory,sampleTimes);
% Assuming aircraftLLA contains the LLA data and sampleTimes are defined
tsec = seconds(0:sampleRate:timeOfTravel(end))';
% Create a timetable directly with LLA data
aircraftTT = array2timetable(aircraftLLA, 'RowTimes', tsec, 'VariableNames', {'Latitude', 'Longitude', 'Altitude'});
% Now use mergevars to merge these variables under a single multicolumn variable
aircraftTT = mergevars(aircraftTT, {'Latitude', 'Longitude', 'Altitude'}, 'NewVariableName', 'AircraftLLA')
aircraftTT = 901x1 timetable
Time AircraftLLA _______ __________________________ 0 sec 38.134 140.45 10000 10 sec 38.134 140.45 10000 20 sec 38.134 140.45 10000 30 sec 38.134 140.45 10000 40 sec 38.134 140.45 10000 50 sec 38.134 140.45 10000 60 sec 38.134 140.45 10000 70 sec 38.134 140.45 10000 80 sec 38.134 140.45 10000 90 sec 38.134 140.45 10000 100 sec 38.134 140.45 10000 110 sec 38.134 140.45 10000 120 sec 38.134 140.45 10000 130 sec 38.134 140.45 10000 140 sec 38.134 140.45 10000 150 sec 38.135 140.45 10000
% Display the first few rows of the modified timetable to verify the structure
head(aircraftTT)
Time AircraftLLA ______ __________________________ 0 sec 38.134 140.45 10000 10 sec 38.134 140.45 10000 20 sec 38.134 140.45 10000 30 sec 38.134 140.45 10000 40 sec 38.134 140.45 10000 50 sec 38.134 140.45 10000 60 sec 38.134 140.45 10000 70 sec 38.134 140.45 10000
Here's what it looks like when you open the timetable from Workspace:
Refer to the documentation to know more: https://www.mathworks.com/help/matlab/ref/table.mergevars.html
Hope it helps!
  1 Comment
Reham Wafaee
Reham Wafaee on 25 May 2024 at 16:49
Hi @Abhishek Kumar Singh, I wanted to accept your answer but I don't know what happened. Thank you a milliooooooooooooooooooooooooooooon, it did solve the big problem that was related to this.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!