Error: subscripted assignment dimension mismatch for loop
2 views (last 30 days)
Show older comments
Hello,
The error I get is : subscripted assignment dimension mismatch at line 40. [line 40 = dataSet(:,1) = str2double(dataArray{1});]
I am trying to loop across a specific number of files in a directory, Everything goes smooth for the first few runs and at random counter values (chopIndex) I get the error. When I did check my workspace, for the iterations where there is no error the size of "dataSet" is 101000x3 double, but post error the size of "dataSet" is 102000x3double.
When I restart and run the code the same issue occurs at a different iteration.
Why does this happen? I'm confused. Please Help.
I have attached my code.
Thank You. SP
0 Comments
Accepted Answer
Geoff Hayes
on 18 Mar 2015
SP - the error message is telling you that you are making an invalid assignment. For example, the following produces the same error
dataSet = zeros(5,1);
dataSet(:,1) = [1:4]';
because we are trying to assign a column of four elements to a column that is expecting five elements.
I suspect that what might be happening is that many of your files which populate dataArray have the same number of rows, so subsequent iterations of your
for i = 1:numOfFiles
allow us to do these assignments
dataSet(:,1) = str2double(dataArray{1});
dataSet(:,2) = str2double(dataArray{2});
dataSet(:,3) = str2double(dataArray{3});
without error because each dataArray has the same number of rows as the dataArray from the previous iteration. This works fine until we encounter an iteration where dataArray has a different number of rows and the error arises.
What you can do, since you don't care to keep the data from one iteration to the next, is just reset dataSet before the assignment. Something like
dataSet = zeros(size(dataArray,1),3);
dataSet(:,1) = str2double(dataArray{1});
dataSet(:,2) = str2double(dataArray{2});
dataSet(:,3) = str2double(dataArray{3});
In the above, we size dataSet to be a matrix with the same number of rows as dataArray with three columns. Try this and see what happens!
3 Comments
Geoff Hayes
on 19 Mar 2015
As I don't have access to your data, it will be easier for you to step through the code using the debugger. Try it and see if you can determine why fMat is different.
More Answers (0)
See Also
Categories
Find more on Cluster Analysis and Anomaly Detection in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!