Error when indexing 3D cell array: Index in position 2 exceeds array bounds

1 vue (au cours des 30 derniers jours)
EDIT: more details provided by myself in the second comment
Hi,
I have a cell array with 19 cells. Within each cell there is a matrix with 21 columns. Now I want to loop a piece of code over each of these columns within the matrices inside each of the cells.
When I run my code I get the error "Index in position 2 exceeds array bounds".
This is the code:
num_cells = size(participants);
num_columns = size(participants{i},2);
block_size = 512;
p_windows = {};
for j = 1:1:num_cells
for k = 1:1:num_columns
N = block_size*ceil(numel(participants{i},2)/block_size);
participants{i,2}(end+1:N) = NaN;
p_windows = reshape(participants{i,2},512,[]);
end
end
My goal is to take each individual column from the matrices in participants and cut them into smaller 512 element long lists. These 512 long lists are then stored in matrices (one matrix per cut-up column) and saved in a new cell array (p_windows).
I dont understand which line this is referring to as it doesn't mention which indexing. Does anyone know how I could fix this?
Thank you!
  2 commentaires
the cyclist
the cyclist le 22 Jan 2022
I tried to guess what the variable participants looks like, to replicate your error, but I couldn't.
Can you please upload the data (using the paperclip icon in the INSERT section of the toolbar)?
Also,
  • This must not be the full code, since it references a variable i that we do not see
  • The full error message will always reference the line number where the error occurs, so you have not told us that either
Please, make it easier for us to help you.
lil brain
lil brain le 22 Jan 2022
Modifié(e) : lil brain le 22 Jan 2022
Thanks for the quick reply!
I tried uploading the variable but its too big (94 mb) but here is a download link:
And here is the full script I am writing:
[file_list, path_n] = uigetfile('.csv', 'Grab csv', 'Multiselect', 'on');
if ~iscell(file_list)
file_list = {file_list};
end
participants = cell(length(file_list),1);
for i = 1:length(file_list)
filename = file_list{i};
data_in = readmatrix([path_n, filename]);
participants{i} = data_in(:,7:27);
end
num_cells = size(participants);
num_columns = size(participants{l},2);
block_size = 512;
p_windows = {};
for j = 1:1:num_cells
for k = 1:1:num_columns
N = block_size*ceil(numel(participants{l},2)/block_size);
participants{l,2}(end+1:N) = NaN;
p_windows = reshape(participants{1,2},512,[]);
end
end

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 24 Jan 2022
Your code is not looping over every cell. It is always using the last cell. This is because the code looping through each cell uses loop counter variable j, but you are indexing participants using i, the counter variable used for loading your files (although in your post with all your code, it uses l, which has not been defined)
You are also not editing the matrix in an existing cell. Instead, you are adding a second column of cells, and assigning them a value of NaN.
Because of this, you are creating a new variable. When creating a variable, you cannot use end as an index.
a{1} = 5;
a{1,2}(end) = nan
Index in position 2 exceeds array bounds. Index must not exceed 1.
Finally, you overwrite p_windows every loop, so in the end, you will only have the resulting matrix from the last cell. I'm not sure I fully understand what you are trying to do, but perhaps this code does what you intended?
p_windows = {};
block_size = 512;
num_cells = length(participants);
for j = 1:num_cells
% Each cell does not have to have the same number of columns
num_columns = size(participants{j},2);
for k = 1:num_columns
if k==1
% Only need to do this once per cell
N = block_size*ceil(numel(participants{j}(:,k))/block_size);
participants{j}(end+1:N,:) = NaN;
end
% Create a cell of cells
p_windows{j,1}{k} = reshape(participants{j}(:,k),512,[]);
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by