# MATLAB/COMSOL geometry loops?

• MATLAB
• luhao
In summary, the conversation discusses creating boxes within a box using MATLAB and Comsol. The user is having trouble with making loops in MATLAB that translate to Comsol's g1=rect2() function. Another user suggests using a while loop and creating a cell array to solve the issue.

#### luhao

Hi I'm a new user in Comsol and Matlab and I have to create boxes within a box. Of course those boxes are easy, my problem is how do you make loops in MATLAB that translates to the g1=rect2(blah blah)
g2=rect2(blah blah)..

For example:
The boxes within the outside box varies so it's better to write everything in a loop say 5 boxes

length=[1,2,3,4,5];
width=[1, 1, 1, 1, 1];
cornerx=[0, 5, 10, 15, 20];
num=5;
for i=1:num;
name=strcat('g',num2str(i));
name=rect2(width(i),length(i),'base','corner','pos',[cornerx,0])
end;

Of course that would not work because the variable name will be overwritten everytime the rect2 occurs. Comsol doesn't follow that name=rect2(width(i),length(i),'base','corner','pos',[cornerx,0]), since it only recognizes g1= , g2=, etc.

Is there any way to write the loop and COMSOL would understand that i want to write 5 or 6 or 7 boxes in a box other than writing out each g1 g2 g3 manually?

Thanks!

Luhao

I also had the same problems with making a loop of my geometries in comsol throug matlab. With a little help from this link:

I manages to make my loop in a while loop, making a cell array :) The problem was that I was not able to use the varible g, i don't know why??!

My script looks like this:

while a > a_internal+(i)*(spac+w)
draw{i} = rect2(w,h,'base','corner','pos',[a_internal+(i-1)*(spac+w),0]);
i = i+1;
end

where a is radius, spac,w,h is different values for my geometry dimention. Then you use acces the structueres with draw{i} where i is the i'th number in your loop..

Hope it helps you:)

## 1. What are geometry loops in MATLAB/COMSOL?

Geometry loops in MATLAB/COMSOL refer to the process of creating and manipulating geometric shapes or objects within the software. These loops allow for the automation of repetitive tasks and can be used to build complex geometric structures.

## 2. How do I create a geometry loop in MATLAB/COMSOL?

To create a geometry loop, you will first need to define the parameters of your loop, such as the number of iterations and the variables to be manipulated. Then, you can use programming techniques such as for loops or while loops to repeat the desired actions for each iteration.

## 3. What are some common applications of geometry loops in MATLAB/COMSOL?

Geometry loops can be used for a variety of purposes, such as creating 3D models, performing geometric transformations, and generating meshes for finite element analysis. They are particularly useful for tasks that involve repetitive geometric operations or require the manipulation of large datasets.

## 4. Can I use geometry loops in MATLAB/COMSOL to optimize my designs?

Yes, geometry loops can be used for design optimization by iterating through different geometric configurations and evaluating their performance. This can help engineers and scientists find the most efficient or optimal design for a particular application.

## 5. Are there any limitations to using geometry loops in MATLAB/COMSOL?

While geometry loops are a powerful tool, there are some limitations to keep in mind. The complexity and size of the geometry may impact the performance of the loop, and it is important to consider the efficiency of the code to avoid long computation times. Additionally, some geometric operations may not be supported within the loop, so it is important to check the documentation for any restrictions.