Add numbers to the matrix
7 views (last 30 days)
Show older comments
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
on 30 Sep 2023
Moved: dpb
on 30 Sep 2023
log=readtable('logfile (8).xlsx')
examp=readtable('logfile_example.xlsx')
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?
Accepted Answer
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);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!