resize and fill the matrix

24 views (last 30 days)
EK
EK on 30 Sep 2023
Commented: Voss on 30 Sep 2023
Hi,
I am trying to resize Excel data matrix and would like to ask if there are easy way to do it in matlab
I have matrices with logfile and data. The first 4 columns is the log fille that logs stimuli representation in time (an example, attached below ). The rows are time and the columns are events. The first column logs stimuli id in time. (No stimulus =0, stimulus : 1 2 3 4 5 or 6) The second column logs the stimulus presentation time and its duration (0= No stimulus, 2=Stimulus); column 3 logs duration and ID of each trial. I need to take given number of raws before and after each stimulus for each trial. For example, 60 rows before stimulus, and 40 rows after the stimulus and reconcatinate the matrix in the same order it was before. I also need to mark in the column 2 Pre stimulus interval ==1 (e.g 60 rows before stimulus == 1) and the Post stimulus interval (e.g 40 rows in after the stimulus == 3) as in example file below. Could anyone help with this? I am attaching logfile and the example files
  2 Comments
Voss
Voss on 30 Sep 2023
This sounds similar to your other question:
Does my recent answer to that question also solve this question? Or are they different tasks?
EK
EK on 30 Sep 2023
It is a bit different here althaough the files are the same. I need to cut trials duration taking given number of raws before and after each stimulus for each trial, fill them with numbers and concatinate back. In some files I do not have trial id log (colomn3) and I need to add stages (Pre stimulus, poststimulus interval) based on number of rows I take before and after the stimulus

Sign in to comment.

Accepted Answer

Voss
Voss on 30 Sep 2023
Edited: Voss on 30 Sep 2023
Try this. It looks for stimulus data (a contiguous section with 2's in column 2), and takes 60 rows before the start and 40 rows after the end of that, and puts the 1's and 3's in place before and after the stimulus section, respectively. It continues doing that until no more stimulus sections are found, then concatenates the sections into a single matrix and writes it to file.
Note that no care is taken to handle the case where 60 rows before stimulus starts is within the previous trial (or before the beginning of the data) or where 40 rows after stimulus ends is within the next trial (or after the end of the data).
input_file = 'logfile-30-9-23.xlsx';
output_file = 'logfile-30-9-23_modified.xlsx';
stim_column = 2;
prestim_value = 1;
stim_value = 2;
poststim_value = 3;
nrows_pre = 60;
nrows_post = 40;
% read input file:
data = readmatrix(input_file);
% initialize empty cell array that will contain trials' data (one trial per cell):
new_data = {};
% start with row 1:
row = 1;
% look for new stimulus data as long as you can:
while true
% find the next stimulus start row:
start_idx = row - 1 + find(data(row:end,stim_column) == stim_value, 1);
% if there is none, we're done, and break out of the loop:
if isempty(start_idx)
break
end
% find the stimulus end row:
end_idx = start_idx - 1 + find(data(start_idx:end,stim_column) ~= stim_value, 1) - 1;
% grab the stimulus data plus 60 rows before and 40 rows after:
trial_data = data(start_idx-nrows_pre:end_idx+nrows_post,:);
% put the pre-stimulus values in place at the beginning:
trial_data(1:nrows_pre,stim_column) = prestim_value;
% put the post-stimulus values in place at the end:
trial_data(end-nrows_post+1:end,stim_column) = poststim_value;
% store the trial's data in the new_data cell array:
new_data{end+1} = trial_data;
% update the row where we'll start looking for the next stimulus section:
row = end_idx + nrows_post + 1;
end
% concatenate the trials' data into a single numeric matrix:
new_data = vertcat(new_data{:});
% write to file:
writematrix(new_data,output_file);
  2 Comments
EK
EK on 30 Sep 2023
Thanks a lot! You are amazing!
Voss
Voss on 30 Sep 2023
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!