How to properly define input types for Matlab Coder?

12 views (last 30 days)
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
tensorisation
tensorisation on 30 Aug 2019
Edited: tensorisation on 30 Aug 2019
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?
Adam
Adam on 2 Sep 2019
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.

Answers (1)

Soumya Paliwal
Soumya Paliwal on 9 Apr 2021
1) How do I define a variable to be 3 dimensional array (the only options I see there are: scalar, vector or matrix)?
  • To define a 3D input using the MATLAB Coder app, first select the datatype, then select any one of the options provided (scalar, vector, matrix). After specifying the first two dimensions, leave a space and type the third dimension. The app will understand that the variable is a 3D variable.
2) How do I define an array with dimensions equal to a certain variable (that is also an input)?
  • If the reference variable is already defined in the workspace, then in the MATLAB Coder app you can specify your variable to be
coder.typeof(<variable_in_workspace>)
  • Alternatively, when specifying the type and value of a variable, you can choose 'Define by Example' and choose the reference variable (present in the workspace) from the list generated.
  • This will ensure that your input argument is of the same data size and same dimensions as the reference variable.
  • For more information on coder.typeof, please refer https://www.mathworks.com/help/fixedpoint/ref/coder.typeof.html

Categories

Find more on Generating Code in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!