Align two data sets
22 views (last 30 days)
Show older comments
Hi,
I am trying to align two datasets log and data matrices (examples attached bellow data sezs of trials). The files have different sizes. Logfile is in ms and the data file is in frames. I need to align them together and downsample the log file to the size of the data file.
The columns in the logfile log the stimuli representation in time (an example, attached below ). The rows are time and the columns are events. The 3d column logs the ID and the duration of the trials. Trial 1=1, Trial2=2 etc. The trials in the log file start at the same time as in the datafile but they are a bit longer than in the datafile. I need to cut each trial at the end to the length of the trial's in the data file, downsample them to the size of the data file and concatenate them back. The trial length in the data file = size(datafile,1)./number of trials. At the moment, I am doing it using the simple code attached below which is limited to 10 trials. I would like to put it in the loop to run it automatically on the files with a large number of trials independent of the number of trials. Could anyone help with this?
1 Comment
MarKf
on 5 Aug 2023
That is not well-written nor working, code and explanation. The unnecessairly long logfile.xlsx seems to have every millisecond logged, associated with the trial ID (or lack thereof for the first 7 ms it seems?), the trial time would be enough (like the variable Trial_IDS) but I guess you need something like that and you need to downsample it.
On failing line 9: tp2=size(datafile,1)./10;% length of the data for one trial I assume that's when your pseudocode starts, and that you mean to extract something from the variable Data, but not the size of the variable like you do in the code, but the trial length. That part is not clear. So not sure how much of the trial you need to cut, and if it's a different time duration for different tirals based on the info in datafile.xlsx.
As for downsampling then just take x(1:50:end,3:4) to get only 1sec/20 = 50ms matrix with tiral ID and time vector, but it won't capture the trial beginning at 7ms.
Accepted Answer
Voss
on 5 Aug 2023
Edited: Voss
on 5 Aug 2023
x = readmatrix('logfile.xlsx');
Data = readmatrix('datafile.xlsx');
frame_rate = 20;
n = 1000/frame_rate;
x(x(:,3) == 0,:) = [];
[g,g_id] = findgroups(x(:,3));
trial_length = size(Data,1)/numel(g_id)*n;
Trials = splitapply(@(t)cut_and_downsample(t,trial_length,n),x,g);
Logfile_concatinated = vertcat(Trials{:});
function out = cut_and_downsample(in,len,n)
% out = {downsample(in(1:len,:),n)};
out = {in(1:n:len,:)};
end
10 Comments
Voss
on 6 Aug 2023
Doesn't frame_rate have to be the actual frame rate that the data was captured at, if you want the two to line up?
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!