How to create a vector inside a loop with data from a struct ?

1 view (last 30 days)
Hello! I'm having some troube making a vector inside a loop with data from a struct. The struc is called :
ECG (1x1 struct with 2 fields)
From these fields, I want to acess a specific one, that is
ECG.event (1×45 struct array with fields:
In the column 'type' I have the triggers from the type char, for exalmple 'S A' , 'S B' , 'S C' , 'S D'. And in the column 'latency' I have the timings they ocured (numeric values). From these fields I want to access data that is in the column 'latency' and 'type'.
I want to know what is the the time difference between 1) a stimulus 'S B' when it is preceeded by a stimulus 'S A', and also the time difference between 2) a stimulus 'S C' when it is preceeded by a stimulus 'S A'.
I need to obtain two vectors, one for each case 1) and 2).
Do you know how I can do it?
Thank you so much in advance! You can find the struc .mat file attached.
  1 Comment
dpb on 16 Oct 2021
Please attach a .mat file with a sample of the struct
Without something to work on it's going to be more difficult to try to recreate a matching structure to test...

Sign in to comment.

Accepted Answer

Stephen23 on 16 Oct 2021
Edited: Stephen23 on 17 Oct 2021
T = {ECG.event.type}; % comma-separated list
L = [ECG.event.latency]; % ditto
xAB = strcmp(T(1:end-1),'A') & strcmp(T(2:end),'B');
xAC = strcmp(T(1:end-1),'A') & strcmp(T(2:end),'C');
d = diff(L);
dAB = d(xAB)
dAC = d(xAC)
Melissa Jones
Melissa Jones on 17 Oct 2021
I didnt tell you because it wasnt needed to separate.

Sign in to comment.

More Answers (0)


Find more on Structures 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!