Pass A Vector (1D Array) To A MATLAB Function In COMSOL With MATLAB

16 views (last 30 days)
I am trying to define the material properties (permittivity and conductivity) of a comsol model with MATLAB functions. The functions have six input arguments. Of the six, first two are usual COMSOL coordinate variables x and y (both are vector) The next three are vectors (1D array) and the sixth one is a scalar.
My approach:
I tried to define the last four variables using the model.param.set() method like following:
model.param.set('x1', x1); % x1 is a vector
model.param.set('y1', y1); % y1 is a vector
model.param.set('epsr1', epsr1); % epsr is a vector
model.param.set('del', del); % del is a scalar
Then I would define the material property as following:
model.component('comp1').material('mat2').propertyGroup('def').set('relpermittivity', {'mat_epsr(x,y,x1,y1,epsr_array,del_c)'}); % mat_epsr is the function
Unfortunately, this throws an error for trying to set an array as a parameter in COMSOL. The error message is as follows:
No method 'set' with matching signature found for class 'com.comsol.clientapi.impl.ModelParamClient'.
Error in (line 36)
model.param.set('x1', x1);
How may I solve this error?

Answers (1)

Gayatri Rathod
Gayatri Rathod on 3 Apr 2023
Hi Md. Al-Imran Abir,
  • The error message suggests that the set method of the ModelParamClient class does not have a matching signature for the arguments you are passing to it. This is likely because you are trying to set an array as a parameter which is not allowed.
  • To solve this error, you can try converting your vectors and scalar to strings using the num2str function. This will allow you to pass them as strings to the set method which should be accepted by COMSOL. Here's an example of how you can modify your code:
x1_str = num2str(x1);
y1_str = num2str(y1);
epsr1_str = num2str(epsr1);
del_str = num2str(del);
model.param.set('x1', x1_str);
model.param.set('y1', y1_str);
model.param.set('epsr1', epsr1_str);
model.param.set('del', del_str);
model.component('comp1').material('mat2').propertyGroup('def').set('relpermittivity', {sprintf('mat_epsr(x,y,%s,%s,%s,%s)', x1_str, y1_str, epsr1_str, del_str)});
  • Here, num2str is used to convert x1, y1, epsr1, and del to strings, which are then passed to the set method. In the set method for the material property, sprintf is used to create a string with the correct format for the mat_epsr function with the converted strings for x1, y1, epsr1 and del substituted into the string using placeholders ("%s").
  • Alternatively, you could also try converting your vectors to cell arrays before calling the set method. Here's an example:
model.param.set('x1', num2cell(x1));
In this case, we are using the num2cell function to convert each element in the vector to a cell, which can be passed to the set method.
  • One more way to do this is to use a loop to set the parameter values for each element of the input vectors. For example, you can modify your code as follows:
% Set x1 and y1 parameters
for i = 1:length(x1)
model.param.set(sprintf('x1[%d]', i), x1(i));
model.param.set(sprintf('y1[%d]', i), y1(i));
end
You can read more about the num2str, set, num2cell and sprintf functions from the following documentations: num2str function, set function, num2cell function, sprintf function.
Hope it helps!  
Regards,
Gayatri Rathod

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!