resize and fill the matrix
24 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Voss
il 30 Set 2023
This sounds similar to your other question:
Does my recent answer to that question also solve this question? Or are they different tasks?
Risposta accettata
Voss
il 30 Set 2023
Modificato: Voss
il 30 Set 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);
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!