MATLAB Answers

1

How to properly define input types for Matlab Coder?

Asked by tensorisation on 30 Aug 2019
Latest activity Commented on by Adam
on 2 Sep 2019
I'm struggling to make a mex file work properly (see more here) so I decided to try and use Matlab Coder to do that hoping that would help. Using Matlab Coder I need to define the inputs, but I encountered 2 issues:
  1. How do I define a variable to be 3 dimensional array (the only options I see there are: scalar, vector or matrix)?
  2. How do I define an array with dimensions equal to a certain variable (that is also an input)?
Additional details:
The Matlab code I'm trying to convert is:
function [V,delta_V_rms,delta_V_abs,delta_V_max] = potentials_evo(L,t_max,N_eff_occ_sites_bounded,i_eff_V_bounded,j_eff_V_bounded,k_eff_V_bounded,system,over_relax_factor,V,delta_V_rms,delta_V_abs,delta_V_max)
% evolution of the potentials %
% note : for the index directions with periodic boundary conditions: index=mod(index-1,L)+1 . for index=index+1 it is mod(index,L)+1 , and for index=index-1 it is mod(index-2,L)+1 %
for t=1:t_max
for q=1:N_eff_occ_sites_bounded % this is set instead of running i=2:(L-1), j=1:L , k=1:L and ending up going over sites that are 0 in our effective system %
i=i_eff_V_bounded(q);
j=j_eff_V_bounded(q);
k=k_eff_V_bounded(q);
V0=V(i,j,k);
V1=( V(i+1,j,k)+V(i-1,j,k)+V(i,mod(j,L)+1,k)+V(i,mod(j-2,L)+1,k)+V(i,j,mod(k,L)+1)+V(i,j,mod(k-2,L)+1) )/( system(i+1,j,k)+system(i-1,j,k)+system(i,mod(j,L)+1,k)+system(i,mod(j-2,L)+1,k)+system(i,j,mod(k,L)+1)+system(i,j,mod(k-2,L)+1) ); % evolving the potential as the average of its occupied neighbors %
V(i,j,k)=V0+(V1-V0)*over_relax_factor; % evolving the potentials in time with the over relaxation factor %
delta_V_rms(t)=delta_V_rms(t)+(V1-V0)^2; % for each t at a given p, we sum over (V1-V0)^2 in order to eventually calculate delta_V_rms_avg %
delta_V_abs(t)=delta_V_abs(t)+abs(V1-V0); % for each t at a given p, we sum over |V1-V0| in order to eventually calculate delta_V_abs_avg %
delta_V_max(t)=max(abs(V1-V0),delta_V_max(t)); % for each t at a given p, we take the max of |V1-V0| from all the sites in order to eventually calculate delta_V_max_avg %
end
end
end
How do I define input for variables like V which is an LxLxL array?

  5 Comments

Well, you can leave it as m x n, which will allow it to be a 2d input of any size, which, when the variable is created to be passed in, could come from another variable in code. You just can't hard-define it in the Coder input argument definition to be of a size that is defined by some other variable.
Can you elaborate and clarify what exactly do you mean by: "you can leave it as m x n, which will allow it to be a 2d input of any size, which, when the variable is created to be passed in, could come from another variable in code.". I'm still not sure what to do.
Also, when I right-click the a variable in the screen where I'm suppost to define inputs, I noticed there a 'Bounded (fixed-size)' marked, and there is also an unmarked 'Bounded (variable-size)' and 'Unbounded'. I tried clicking these options but nothing happens and the 'Bounded (fixed-size)' option is still marked. Are these options relevant to this, and how do I use them?
Ah yes, it's a while since I used it. If you enter :Inf as the dimension size then it is allowed to be anything, so
:Inf x :Inf x :Inf
will be a 3d array of any size. You would then validate the actual size somewhere else if you wish to do this. Usually I have a .m wrapper file around mex functions I create that does any input validation I need so that the C or C++ code just works on the general case, unless I can hard-code sizes (e.g. if something should always be a 3x3x3 array)

Sign in to comment.

Products


Release

R2018b

0 Answers