Use a cell array in a for loop

10 views (last 30 days)
Emma Pearce
Emma Pearce on 29 Jan 2018
Answered: Guillaume on 29 Jan 2018
Hi,
I want to be able to loop through data in a nested loop and create an array of values each time I loop round, and store this array in a cell array.
My code is currently:
nrms_data = zeros(48,67); %empty list 48 rows and with 67 columns spaces
for i =(1:67) %this will circle through all the gathers
for j = (1:48)%this will cycle through all the traces in the data for the individual gather
trace_j = data{i}(:,j); % gives j'th trace of the gather_i
good_trace = data{17}(:,j);
NRMS = nrms(good_trace, trace_j);
nrms_data{i}(j)= NRMS;
I thought that this would store the value of NRMS as the j'th column in the cell array nrms_data but the error is
Cell contents assignment to anon-cell array object.
I'm unsure how else to do this. I want to be able to create an array of nrms values for each ith gather and each jth trace and then have a cell array that is 67 columns long with 48 rows.

Accepted Answer

Guillaume
Guillaume on 29 Jan 2018
Well yes, you declare nrms_data as a matrix of double, not a cell array. In addition, nrms_data{i}(j) access the scalar element at position j in the matrix of cell i, so that's not what you want.
nrms_data = cell(48, 67);
for i = 1:67
%...
nrms_data{i, j} = NRMS;

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!