필터 지우기
필터 지우기

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

조회 수: 4 (최근 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에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by