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

1 Ansicht (letzte 30 Tage)
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:
latency
type)
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 Kommentar
dpb
dpb am 16 Okt. 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...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 16 Okt. 2021
Bearbeitet: Stephen23 am 17 Okt. 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)
  2 Kommentare
Stephen23
Stephen23 am 17 Okt. 2021
Bearbeitet: Stephen23 am 17 Okt. 2021
"Can you please explain the xAB and xAC logic please?"
They are logical vectors (i.e. only boolean true/false values) which are true only when 'A' immediately precedes 'B' or 'C' respectively, as you requested in your original question, within the cell array T. They use indexing to compare the content of adjacent cells of the cell array T. Due to that indexing, they each have numel(T)-1 elements (which is exactly the same number of elements d has).
"It gives me a 1×717 logical array only with 0s for each case (xAB and xAC)."
Assuming that this is true, then in no cases do you have 'A' immediately preceding 'B' or 'C'.
Lets take a closer look at your data:
S = load('ECG.mat')
S = struct with fields:
ECG: [1×1 struct]
ECG = S.ECG
ECG = struct with fields:
event: [1×45 struct]
T = {ECG.event.type}
T = 1×45 cell array
{'boundary'} {'S A'} {'S B'} {'S 55'} {'S A'} {'S C'} {'S 9'} {'S 10'} {'S 99'} {'S A'} {'S C'} {'S 55'} {'S 6'} {'S 8'} {'S A'} {'S 10'} {'S 99'} {'S A'} {'S B'} {'S 55'} {'S 6'} {'S 7'} {'S 9'} {'S 10'} {'S 99'} {'S A'} {'S C'} {'S 55'} {'S 6'} {'S 8'} {'S 9'} {'S 10'} {'S 99'} {'S A'} {'S B'} {'S 55'} {'S 6'} {'S 7'} {'S 9'} {'S 10'} {'S 99'} {'S A'} {'S C'} {'S 55'} {'S 6'}
Interesting... you did not tell us that there are other characters, and sadly computers cannot read your mind (nor I) and know that you want to ignore those various 'S' characters, space characters, and perhaps the entire text of "the cat in the hat". Computers (like me) are very stupid and can only do what they are told to do, so you have to give them very clear and precise instructions (including what to ignore).
Assuming that you want to ignore all of those other characters, then we could do this:
T = regexprep({ECG.event.type},'[^ABCD]','')
T = 1×45 cell array
{0×0 char} {'A'} {'B'} {0×0 char} {'A'} {'C'} {0×0 char} {0×0 char} {0×0 char} {'A'} {'C'} {0×0 char} {0×0 char} {0×0 char} {'A'} {0×0 char} {0×0 char} {'A'} {'B'} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'A'} {'C'} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'A'} {'B'} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'A'} {'C'} {0×0 char} {0×0 char}
xAB = strcmp(T(1:end-1),'A') & strcmp(T(2:end),'B');
xAC = strcmp(T(1:end-1),'A') & strcmp(T(2:end),'C');
nnz(xAB)
ans =
3
nnz(xAC)
ans =
4
L = [ECG.event.latency];
d = diff(L);
dAB = d(xAB)
dAB = 1×3
566.2500 597.0000 484.7500
dAC = d(xAC)
dAC = 1×4
1.0e+03 * 1.0043 0.7215 0.1492 0.0975
There are other approaches, e.g. using REGEXP or CONTAINS or STRFIND or loops or ... etc.
Melissa Jones
Melissa Jones am 17 Okt. 2021
I didnt tell you because it wasnt needed to separate.
THANK YOU SO MUCH!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by