Add numbers to the matrix

7 views (last 30 days)
EK
EK on 30 Sep 2023
Commented: Voss on 30 Sep 2023
Hi,
I am trying to refill excel matrix with the numbers and would like to ask if there are easy way to do it in matlab
I have matrices of 4 columns that log stimuli representation in time (logfile, 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. Each trial consists of pre-stimulus ,stimulus, and post-stimulus intervals. I need to add in 2d column numbers that would code for pre- and post-stimulus intervals for each trial as in example file attached below. Pre stimulus interval =1 (time from beginning of each trial till the stimulus presentation ) ; Post stimulus interval =3 (time after the end of the stimulus till the end of the trial). The log file and example file attached below
Could anyone help with this?
  3 Comments
dpb
dpb on 30 Sep 2023
Moved: dpb on 30 Sep 2023
log=readtable('logfile (8).xlsx')
log = 28000×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 0 0 1 1 0 0 1 2 0 0 1 3 0 0 1 4 0 0 1 5 0 0 1 6 0 0 1 7 0 0 1 8 0 0 1 9 0 0 1 10 0 0 1 11 0 0 1 12 0 0 1 13 0 0 1 14 0 0 1 15 0 0 1 16
examp=readtable('logfile_example.xlsx')
examp = 25480×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 0 1 1 1 0 1 1 2 0 1 1 3 0 1 1 4 0 1 1 5 0 1 1 6 0 1 1 7 0 1 1 8 0 1 1 9 0 1 1 10 0 1 1 11 0 1 1 12 0 1 1 13 0 1 1 14 0 1 1 15 0 1 1 16
Why is the example 25K where the other is 28K records?
Instead of 28K sized file that is far too big to be able to see easily what is going on, create a SMALL demo file of 50 records or less that illustrates the input and desired output and, as @Dyuman Joshi says, we need a defining logic statement that can be coded to set this other value -- for starters, it seems to be reusing the same indicator value for this pre-trigger and post-trigger indicator as is being used as a stimulus indicator, so how are they to be distinguished?
EK
EK on 30 Sep 2023
the only difference between the files is that the 2d file has different length of the trials but the logic is the same. The length is not relevant here. The logfile has 140 trials indicated in the column 3. The trial length is 200 rows in logfile and 182 in example file. You can just look at the first trial (first 182 rows in columns 2 and 3) in the example file. The pre stimulus interval==1 starts from the beginning of each trial (column 3) till the stimulus start ==2 (in column2). If you look at first trial it starts from 1: 70 (column 2). The post-stimulus interval==3 starts from the end of each stimulus (column 2) till the end of the trial (in column3). 113:182 (column 2) for the first trial and so on.

Sign in to comment.

Accepted Answer

Voss
Voss on 30 Sep 2023
Edited: Voss on 30 Sep 2023
Here's one way:
stim_column = 2;
trial_column = 3;
prestim_value = 1;
stim_value = 2;
poststim_value = 3;
input_file = 'logfile (8).xlsx';
output_file = 'logfile (8)_modified.xlsx';
% read input file:
data = readmatrix(input_file);
% get trial id numbers and which trial each row of the data matrix belongs to:
[trial_ids,~,locs] = unique(data(:,trial_column));
% for each trial ...
for ii = 1:numel(trial_ids)
% idx is a logical vector indicating which rows of data belong to trial ii:
idx = locs == trial_ids(ii);
% data from stim column (column 2 in this case) for trial ii:
stim_data = data(idx,stim_column);
% index within this trial's data where stimulus starts:
start_idx = find(stim_data == stim_value,1);
% index within this trial's data where stimulus ends:
end_idx = find(stim_data == stim_value,1,'last');
% fill in values before stimulus start with prestim_value (1):
stim_data(1:start_idx-1) = prestim_value;
% fill in values after stimulus end with poststim_value (3):
stim_data(end_idx+1:end) = poststim_value;
% put new trial stimulus data back in place in the full matrix:
data(idx,stim_column) = stim_data;
end
% write the new data to file:
writematrix(data,output_file);
  2 Comments
EK
EK on 30 Sep 2023
Thank you so much!
Voss
Voss on 30 Sep 2023
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!