Inserting matrices in a cell - in a for loop

1 visualizzazione (ultimi 30 giorni)
I have to create eleven matrices with fiber photometry data coupled with occurrence of eleven stimuli, to understand how each of them affects neural activity. After that, I want to store them in a cell. This is the outcome that I'm aiming for:
In this case, there was no data to record for stimuli from 6 to 11, but that's ok.
After 90 lines of code, this is the part where I try to put data in those matrices. However, with the same exact file, what I get is:
Inside each of those cells:
So, a) it looks like data are shifted towards right b) instead of having matrices within a cell, I have matrices within cells within a cell.
This is the correct code:
dff1 = [];
dff2 = [];
dff3 = [];
dff4 = [];
dff5 = [];
dff6 = [];
dff7 = [];
dff8 = [];
dff9 = [];
dff10 = [];
dff11 = [];
for j = 1:length(num_ind)
if num_ind(j)*Fs+epochlength > length(Activity)
fprintf(' %d index is out of range of our read\n',j) ;
else
num_activity(:,j) = Activity_dff(num_ind(j)*Fs-epochlength:num_ind(j)*Fs+epochlength);
end
end
[r,c] = size(num_activity);
num_dff = num_activity;
if code_events(i) == 1
if length(dff1) ==0
dff1=[dff1,num_dff];
else
dff1=[dff1;num_dff];
end
elseif code_events(i) == 2
if length(dff2) ==0
dff2=[dff2,num_dff];
else
dff2=[dff2;num_dff];
end
And so on until eleven. Finally:
wholedff={dff1,dff2,dff3,dff4,dff5,dff6,dff7,dff8,dff9,dff10,dff11};
wholedff(1) = {dff1}
I tried to improve that code with this:
wholedff = cell(1, 11);
for c_event = 1:length(code_events)
%Declare variable
num_code = [];
num_ind = [];
num_activity = [];
num_activity_mean = [];
num_dff = [];
if code_events(c_event) == 0
disp("Preventing unwanted parse");
else
num_code = find(a.events == code_events(c_event));
num_ind = a.event_times(num_code);
%Count number of events
fprintf('Number of %d event is %d\n',code_events(c_event), length(num_code))
%Getting the photometry points
for j = 1:length(num_ind)
if num_ind(j)*Sampling_rate+Epoch_length > length(Activity)
fprintf('%d index is out of range of our read\n',j) ;
else
num_activity(:,j) = Activity_dff(num_ind(j)*Sampling_rate-Epoch_length:num_ind(j)*Sampling_rate+Epoch_length); %-20 seconds +20seconds of the event time
end
end
[~,c] = size(num_activity);
num_dff = num_activity;
if isempty(wholedff{c_event})
wholedff{c_event} = [wholedff(c_event),num_dff];
else
wholedff{c_event} = [wholedff(c_event);num_dff];
end
end
end
dff1 = cell2mat(wholedff{1});
dff2 = cell2mat(wholedff{2});
dff3 = cell2mat(wholedff{3});
dff4 = cell2mat(wholedff{4});
dff5 = cell2mat(wholedff{5});
dff6 = cell2mat(wholedff{6});
dff7 = cell2mat(wholedff{7});
dff8 = cell2mat(wholedff{8});
dff9 = cell2mat(wholedff{9});
dff10 = cell2mat(wholedff{10});
dff11 = cell2mat(wholedff{11});
However, it's clearly not working

Risposta accettata

Stephen23
Stephen23 il 2 Mag 2020
Modificato: Stephen23 il 2 Mag 2020
You need to get the data out of the cell array before concatenating it with the new data, i.e. replace
wholedff{c_event} = [wholedff(c_event),num_dff]
with
wholedff{c_event} = [wholedff{c_event},num_dff]
% ^ ^ correct indexing

Più risposte (0)

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by