Coder Size Mismatch error

조회 수: 1 (최근 30일)
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju 2021년 4월 3일
편집: Naga Manoj Kumar Lakkoju 2021년 4월 4일
Hello All,
Here is the small function i want to convert to C using Matlab coder.
function []=fun()
x=ones(9,11).*[0:10:100];
var1 = 0;
for t=11:500
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
end
end
I am getting size mismatch error: [9,11] ~=[1,11] in Matlab coder.
Things I tried
function []=fun()
x=bsxfun(@times, ones(9,11),[0:10:100]); % Changed this line
var1 = 0;
for t=11:500
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1; % error in this line
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
if sum(x(:,t+1))/9<100-5
x(:,t+1)=x(:,t+1)+5;
end
end
end
Error : Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x.
function []=fun()
x=bsxfun(@times, ones(9,11),[0:10:100]);
x=repmat(x,[1,1290]) % Changed this line
var1 = 0;
for t=11:1290
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
if sum(x(:,t+1))/9<100-5 % error in this line
x(:,t+1)=x(:,t+1)+5;
end
end
end
Error: Sizes mismatch: [1290][9] ~= [14179][9]. in coder and also it is not correct because in my main file dimension of x and other variable is not matching. So I think it is better not to use repmat.
Please give your suggestions. I will try to apply in my code.
Thank You
Manoj
  댓글 수: 7
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju 2021년 4월 4일
편집: Naga Manoj Kumar Lakkoju 2021년 4월 4일
Yeah, Got it. Thank for the questions . Here is updated code.
coder.varsize('x');
x=zeros(9,1300);
x=bsxfun(@times,ones(9,11),[0:10:100]);
x=[x, zeros(9,1279)];
% Not related to previous code.
I have one more doubt
Let say, I have a structure, and When I try to generate code in Matlabcoder it is showing error
If i apply same questions here, a is executed 10 times which means it will have 10 instances. So do I need to pre allocate all the 10 fields? If that is the case then it would be messy if we have more filelds or max loop is so high. there must some way to do this
dpb
dpb 2021년 4월 4일
I'm not all that familiar with the coder, but look at/read the documentation thoroughly first...
<Coder controlling-memory-allocation> looks like a good starting point.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by