Error: Index in position 1 exceeds array bounds (must not exceed 1).
    2 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hi all,
I am trying to import individual matlab files size 1x403, and wanting to put all 26 of these files(individual subjects data of 403 values) into one bid matrix size 26 (columnsx 403 (rows). 
I have created a function that works; by using a for loop to import the data based on the file naming; see below
function c = alldata_save()
%reading files from sub-01 to sub-30; files are in current directory
%for DR_corr results, want to save into 
home_path = '/Users/pdclinic/Documents/MATLAB/Natasha/out/Time_Extraction_fMRI/DR_Corr';
dr = []; % create matrix named dr
%a = [1:30]; %all subjects
%b = [12,17,19,24]; %excluded values from subjects
%c = setdiff(a,b); %subjects that are included (without excluded ones)
for d=1:2  % variables we don't want [12 17 19 24]%create loop through data for participants files  
     if d <10 %conditions of naming the file for subject
   abc = sprintf('%s%d%s','subnum = ''sub-0',d,'''');
    eval(abc) %if below 10, it will be sub-01 
     else
      abc = sprintf('%s%d%s','subnum = ''sub-',d,''''); %subnum = ['sub-' i] %if above 10, it will be sub- 
    eval(abc)
    end
    filename = sprintf('%s-dr_corr.mat',subnum);%filename is subject number up to in loop
    dr = importdata(filename),d(403, 1:2); %import data, 403 variables per 1 subject
    % currently index in position 1 exceeds array bounds (must not exceed 1), i need it to grow dynamically, need the array         to grow to 403 x 26 (26 subjects)
end
save('alldr_corr.mat');
However, the error occurs at line dr=importdata(filename), d(403,1:2). I have put d=1:2 (is because I was just trialing for 2 participants, the error that always occurs is 
Index in position 1 exceeds array bounds (must not exceed 1).
Error in alldata_save (line 21)
    dr = importdata(filename), d(403,2); 
How do I fix this error, so the array will expand with the loop to include the next subject data into the next column (matrix needs to grow)? 
Thank you for your help.
0 Commenti
Risposte (1)
  Athul Prakash
    
 il 27 Set 2019
        Hey Natasha,
The Problem
Variable 'd' is the index variable used in the for loop. It does not behave as a vector at all, it behaves like a scalar: 'd' takes each value in the given vector (i.e. '1:2') one by one, each iteration. So 'd' takes value 1 and 2 inside the for loop. So you can't index into d(403, 1:2) since 'd' is a scalar.
Try this
for d = setdiff(1:30, [12 17 19 24])
        %calculate 'filename' based on d
        dr(:, d) = importdata(filename);
end
MATLAB creates a matrix dr and in every iteration, populates the d'th column with 403 values. 
Do remember that you will get zeros in columns [12,17,19,24] since 'd' will skip those values. You can remove those after the loop has run.
dr(:, [12 17 19 24]) = [ ]; % remove select columns
Hope it helps!
0 Commenti
Vedere anche
Categorie
				Scopri di più su Matrix Indexing 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!

