Assigning Columns to a table without using loops - and perform i.e. diff on a column
75 views (last 30 days)
Show older comments
Hello, I have an events log that I want to be able to pull out relevant times and types of triggers into an array of some sort. As its a mixture of text and numbers I originally thought of using a cell array, but have since preferred the idea of using a table.
Ultimately I want a table showing
[count, FrameNumber, (AbsTime-AbsTime_from_1st event) trigger type
All of this info is available in the events.Data and events.type fields
This was my basic attempt to get going
Type=events(:).Type
AbsTime=datetime(events(:).Data.AbsTime)
T0=datetime(events(1).Data.AbsTime)
D=diff(T0)
table(Type,AbsTime,T0,D)
But I see an error
Intermediate dot '.' indexing produced a comma-separated list with 5 values, but it must produce a single value when followed by subsequent indexing
operations.
Error in HTS_TestSoftware/StopVidButtonPushed (line 8572)
AbsTime=datetime(events(:).Data.AbsTime)
Does this mean I have to use loops then?
Also how do I include the diff of the abs time to get each individual time difference?
events =
1×5 struct array with fields:
Type
Data
ans =
struct with fields:
% Here is events.Data-----------------------------------------------
AbsTime: [2025 2 14 15 22 54.8366]
FrameNumber: 0
RelativeFrame: 0
TriggerIndex: 0
ans =
struct with fields:
AbsTime: [2025 2 14 15 22 55.0519]
FrameNumber: 0
RelativeFrame: 0
TriggerIndex: 0
ans =
struct with fields:
AbsTime: [2025 2 14 15 22 55.1534]
FrameNumber: 1
RelativeFrame: 0
TriggerIndex: 1
ans =
struct with fields:
AbsTime: [2025 2 14 15 22 55.2520]
FrameNumber: 2
RelativeFrame: 0
TriggerIndex: 2
ans =
struct with fields:
AbsTime: [2025 2 14 15 22 55.4438]
FrameNumber: 3
RelativeFrame: 1
TriggerIndex: 3
% Here is events.Type-----------------------------------------------
ans =
'Start'
ans =
'Trigger'
ans =
'Trigger'
ans =
'Trigger'
ans =
'Stop'
Type =
'Start'
2 Comments
Cris LaPierre
on 14 Feb 2025 at 16:40
Please attach your data file and the code you are using to import it.
It is hard to provide a solution specific to your use case without having your data.
Accepted Answer
Voss
on 14 Feb 2025 at 16:59
A struct array similar to your variable events:
tmp = num2cell([2025 2 14 15 22 54.8366; 2025 2 14 15 22 55.0519; 2025 2 14 15 22 55.1534; 2025 2 14 15 22 55.2520; 2025 2 14 15 22 55.4438],2).';
events = struct( ...
'Type',{'Start','Trigger','Trigger','Trigger','Stop'}, ...
'Data',num2cell(struct('AbsTime',tmp,'FrameNumber',{0,0,1,2,3},'RelativeFrame',{0,0,0,0,1},'TriggerIndex',{0,0,1,2,3})))
events(1), events(1).Data
events(end), events(end).Data
One way to build something like the table you're trying to build:
Type = {events.Type}.'
Data = [events.Data]
AbsTime = datetime(vertcat(Data.AbsTime))
% T0 = datetime(events(1).Data.AbsTime)
T0 = AbsTime(1) % same
DT = AbsTime-T0
DT.Format = 'mm:ss.SSSS'
count = (1:numel(events)).';
FrameNumber = vertcat(Data.FrameNumber);
T = table(count,FrameNumber,DT,Type)
10 Comments
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!