How to extract trajectories based on trial times from another array?

4 vues (au cours des 30 derniers jours)
Alex Willcox
Alex Willcox le 6 Juil 2020
Réponse apportée : Deepak le 18 Oct 2024
I have an array formed of three columns: 1- time (in seconds with 0.05ms intervals e.g. 0.05, 0.1, 0.15 ms ect...), 2- X coordinate (of paw in pixels) and 3- Y coordinate (of paw in pixels). I have another array with two columns, 1- behviour type (can be start of trial, contact with target or end of trial) and 2- the time in seconds corresponding to that behaviour.
I would like to extract the X and Y coordinates from the first array using the trial start and stop times from the second array, to get paw trajectory for each trial (roughly 100 per file). Does anyone have a suggestion of how I could go about this? My main issue is figuring out how to loop through the start and stop times. I'm thinking I'll have to identify each start time individually and extract/plot trajectory to the stop time following that start and loop through the second variable like that but I'm unsure on how to actually do that. Thanks in advance!

Réponses (1)

Deepak
Deepak le 18 Oct 2024
Hi Alex,
As I understand it, you are working with two arrays, one containing time, X, and Y coordinates of a paw’s movement, and another detailing behaviour (like start and end of trial) along with their corresponding times. You want to extract the X and Y coordinates for each trial based on the start and end times specified in the behaviour array, as there are about 100 trials per file.
To achieve this, we can iterate over the behaviour array to find “start” entries that captures the corresponding start times. Within this loop, we can use a nested loop to search for the next "end" behaviour to determine the end time. We can then identify the indices in the trajectory array that fall within this time range and extract the X and Y coordinates, storing them in the cell array. Finally, we can plot each trial’s trajectory to visualize.
Please find below the MATLAB code that accomplishes this task:
% trajectoryArray (Nx3): [time, X, Y]
% behaviorArray (Mx2): [behaviorType, time]
% Preallocate a cell array to store trajectories
trajectories = cell(1, 100);
numTrials = size(behaviorArray, 1);
for i = 1:numTrials
% Identify start and stop times
if strcmp(behaviorArray(i, 1), 'start')
startTime = behaviorArray(i, 2);
% Look for the corresponding 'end' entry
for j = (i+1):numTrials
if strcmp(behaviorArray(j, 1), 'end')
endTime = behaviorArray(j, 2);
break;
end
end
timeRange = trajectoryArray(:, 1);
indices = timeRange >= startTime & timeRange <= endTime;
trajectories{i} = trajectoryArray(indices, 2:3); % [X, Y]
figure;
plot(trajectories{i}(:, 1), trajectories{i}(:, 2));
title(['Trial ', num2str(i)]);
xlabel('X (pixels)');
ylabel('Y (pixels)');
grid on;
end
end
Please find attached the documentation of functions used for reference:
I believe this will help in resolving the issue.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by