MATLAB Answers

Array indices must be positive integers or logical values.

조회 수: 10(최근 30일)
GA 26 Feb 2020
편집: GA 26 Feb 2020
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);
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;
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!

  댓글 수: 1

Walter Roberson
Walter Roberson 26 Feb 2020
Perhaps charpath is a variable instead of a function?

로그인 to comment.


James Tursa
James Tursa 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

GA 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 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.

로그인 to comment.

Star Strider
Star Strider 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.

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by