Array indices must be positive integers or logical values.

1 view (last 30 days)
Hello,
I'm trying to create a loop that will add each file (name) calculation of density, transitivity, small_world ( from brain connectivity toolbox) to a zeros matrix called final_CCN.
% create zeros matrix
final_CCN = zeros(length(name),3);
name_extracted = extractBefore(name,34);
name_extracted=extractAfter(name_extracted,26);
ex= cell2table(name_extracted);
final_CCN = [name_extracted, array2table(final_CCN)];
final_CCN.Properties.VariableNames = {'Name', 'density', 'transitivity', 'small_world'};
% loop that calculates variables of interest
for i = 1:length(name)
R = readtable(sprintf(%s%s, char(directory),char(name(i)), 'ReadRowNames', false);
A = table2array(R);
density = density_und(A); % density column variable
transitivity = transitivity_wu(A); % transitivity column variable
rando = null_model_und_sign(A);
C = clustering_coef_wu(A);
Crand = clustering_coef_wu(rando);
meanC = mean(C);
meanCrando = mean(Crando);
W = weight_conversion(A, 'lengths');
Wrando = weight_conversion(rando, 'lengths');
D = distance_wei(W);
Drand = distance_wei(Wrando);
L = charpath(D);
Lrand = charpath(Drand);
clust = meanC/meanCrand;
CPL = L/Lrand;
small_world = clust/CPL; % small_world column variable
final_CCN.density(i) = density;
final_CCN.transitivity(i) = transitivity;
final_CCN.small_world(i) = small_world;
end
When I use this code, only the first line of the zeros matrix filled in, and the rest of the file (names) don't seem to be going through the loop. After running the code I get the error: Index exceeds the number of array elements (1).
Can someone please help me with this? Thanks so much!

Answers (2)

James Tursa
James Tursa on 26 Feb 2020
These lines:
L = charpath(D);
Lrand = charpath(Drand);
clust = meanC/meanCrand;
charpath = L/Lrand;
look like you are using charpath as both a function and as a variable. Which is it? The error you are getting indicates it is a variable in the workspace but you are treating it like a function call with non-integer input.
  2 Comments
GA
GA on 26 Feb 2020
Thank you, yes that's true!
charpath is the function, so I changed the variable name. Now I get the error "Index exceeds the number of array elements (1)." Do you know why? I'm still having trouble adding the rest of the file (names) to the matrix.
Thanks a lot for your help.
James Tursa
James Tursa on 26 Feb 2020
Edited: James Tursa on 26 Feb 2020
Clear your workspace of everything and run the script from scratch. If you still get an error, then post your current code and then copy & paste the entire error message (including the offending line) so that we don't have to guess.

Sign in to comment.


Star Strider
Star Strider on 26 Feb 2020
Whatever ‘charpath’ was originally (array? function?) is now over the horizon so we cannot see it.
This line:
charpath = L/Lrand;
defines it now and subsequently as a variable, so atttempting to index it (beyond a value of 1) or pass arguments to it (at all) willl throw the error you got.
This is called ‘overshadowing’ and is not considered to be good programming practice.
The solution is to decide what you want to do with ‘charpath’ and what you want it to be, and then not change it.

Tags

Community Treasure Hunt

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

Start Hunting!