Index in position 2 exceeds array bounds (must not exceed 1). Error in dlmread (line 159) result= result(:,1:ncols);

2 views (last 30 days)
In my try.txt file, there are 9 columns with 300000 float values(300 times 1000 samples). In between every 1000 samples and in the beginning, there is some text written which I tried to skip by incrementing i appropriately. My aim is to find variance of every column with 1000 samples and repeat till the end(i.e will 300000), which will enable me to get 300 by 9 matrix.
clc
clear
close all
file=('try.txt');
%M = dlmread(testtime.txt,' ',50,50)
i=23;
%variences=zeros(300,1);
for k=1:300
j=i+1000-1;
A = dlmread(file,' ',[i 0 j 8]);
variences(k)= var(A(:,3));
i=i+1000+11;
end
variences';
plot(1:300,variences)
variences=variences';
Please help me asap. I am new to matlab

Answers (1)

Jon
Jon on 19 Nov 2020
You may have other problems too, but I noticed that since i does not change inside of your loop, you always read from the same starting row. Note that in your call i which is the first (starting row) does not change, but you ending row j does. Instead I would think you want something more like
A = dlmread(file,' ',[j 0 j+1000 8]);
  5 Comments
Jon
Jon on 20 Nov 2020
Edited: Jon on 20 Nov 2020
When you calculate the variances you want to calculate them for each of the 9 columns in A. Also you need to assign the whole resulting 9 element vector of variances to an array to hold the results for that block of data . You should preallocate your array variances before the loop to efficiently allocate space and also to ensure that there isn't anything using already assigned to that array (even though you do a clear at the beginning, still good practice) See if this gets closer. Indexing is tricky with the offsets so please confirm that I got this right and it is consistent with what is in your files
clc
clear
close all
% assign parameters at beginning to avoid "magic numbers" appering in code
numHeaderRows = 23; % number of initial header rows
numBlocks = 300; % number of data blocks
numDataRows = 1000; % number of data rows in a block
numCols = 9; % number of data columns
numTextRows = 12; % number of text rows between blocks
% assign name of file holding the data
file='try.txt';
% prepare to loop through data
% assign initial offset to first row of data to be read
i=numHeaderRows - 1; % initial offset (zero based)
variances=zeros(numBlocks,numCols); % preallocate array to hold calculated variances
for k=1:numBlocks
% assign ending offset for block
j=i+1000-1;
% read in block of data
A = dlmread(file,' ',[i 0 j numCols-1]);
% calculate variances
variances(k,:)= var(A(:,3)); % assign to current row in result matrix
% increment starting offset for next block
i = i + numDataRows + numTextRows;
end
% plot results
plot(1:numBlocks,variances)
% save variances as numberCols by numBlocks array
variances = variances';
Jon
Jon on 20 Nov 2020
Edited: Jon on 20 Nov 2020
If this still is not working then please attach a portion of your data file with the first couple of blocks of data (so about 2000+ rows, not too big but big enough to see exactly what is in there and the repetitive pattern)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!