Error: Index exceeds array dimensions. Index value 2 exceeds valid range [1-1] of array a.

조회 수: 1 (최근 30일)
Hello All, I am converting matlab script to C++.
Here is a test function which is similar to my project. When i try to generate C code using Matlab coder it is throwing index exceeds its valid range. What is the meaning of this error. Could anyone guide me how to fix this, please.
function [a]=test()
% a=repmat(struct('in1',zeros(1,5),'in2',ones(1,5)),1,5);
years = 1950:10:1990;
service = 10:10:30;
wage = [150.697 199.592 187.625
179.323 195.072 250.287
203.212 179.092 322.767
226.505 153.706 426.730
249.633 120.281 598.243];
for i = 1:5
a(i).in1=interp2(service,years,wage,15,1975);
a(i).in2=interp2(service,years,wage,15,1970);
end
  댓글 수: 5
Jan
Jan 2021년 4월 8일
What happes if you pre-allocate the struct array a? You show some outcommented code already. Or:
a = struct('in1', cell(1,5), 'in2', cell(1,5));
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju 2021년 4월 8일
Yes, it is showing size mismatch error. I will post it here. For this error we have to say something like
coder.varsize('a.in1')
coder.varsize('a.in2')
yes, Now it is working fine.
May I ask you why you have used cell instead of zeros or ones.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by