How to solve "Attempted to access index 12 of an array with smaller dimension sizes" problem?

3 次查看(过去 30 天)
Hi. I get an error "Attempted to access index 12 of an array with smaller dimension sizes".
This error occurred when I change 'constant block' with 'from file block'.
In the constant block, I have a set of data like this...
[35.85
35.74
36.03
35.36
35.36
35.74
36.03
35.95
36.03
35.82
35.83
35.25
35.9
36.07
36.2
35.92
35.66
35.94
36.02
35.94
35.92
35.84
36.2
36.28
]
and in the from file block the data is just the same as the data above.
Here is my code in the MATLAB function block.
function Consistency = detection(x)
H = x;
S = std(H(12:24,:));
Consistency = S;
end
Please give your suggestion.
Thank you.
  4 个评论
Walter Roberson
Walter Roberson 2016-10-11
function Consistency = detection(x)
assert(size(x,1) >= 24);
H = x;
S = zeros(1, size(x,2));
S = std(H(12:24,:));
Consistency = S;
end
If the assert() fails then the problem would be that you did not have at least 24 rows.
SyukriY
SyukriY 2016-10-12
Hi, Walter Roberson.
As you assume, the assert fails. But, I have 24rows in the file. What could be wronged?

请先登录,再进行评论。

采纳的回答

Preethi
Preethi 2016-10-12
Hi,
Save the data in .mat as columns, use transpose at the start of function.
The file must contain a matrix of two or more rows. The first row must contain monotonically increasing time points. Other rows contain data points that correspond to the time point in that column. The matrix is expected to have this form.
<http://www-rohan.sdsu.edu/doc/matlab/toolbox/simulink/slref/fromfile.html>
  3 个评论
Preethi
Preethi 2016-10-12
hi,
'From file' block reads first row as time stamp and the data in other rows as values corresponding to that time stamp. in your case the output of the block will contain only two samples of data, hence the error. Just transpose the data and save again in .mat file, it will 2 x 24.
Inside your code use x =x.' as first line and then check.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2016-10-12
When you use From Workspace to bring in a matrix (I have not tested From File as much):
Rows of the variable correspond to timepoints. Column 1 will be used for the corresponding time. For each timepoint, a row without the first column will be passed to the function, but the row will be passed as a column vector. For example, for a 25 x 30 matrix, since the first row is time, there are 25 x 29 remaining data, and each function call will be passed a column of 29 values, with the function being called multiple times.
If you are trying to do this at initialization time, a one-time calculation with all of the data in the matrix, with the output becoming more or less a parameter for the run, then I am not sure if that is possible, but I would not rule it out (I do not have the experience to say.)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by