How to Create Geometry Loops in MATLAB/COMSOL?

  • Context: MATLAB 
  • Thread starter Thread starter luhao
  • Start date Start date
  • Tags Tags
    Geometry Loops
Click For Summary
SUMMARY

This discussion focuses on creating geometry loops in MATLAB for use with COMSOL, specifically generating multiple rectangular boxes within a larger box. The user, Luhao, encountered challenges with variable naming in loops, as COMSOL requires explicit variable names like g1, g2, etc. A solution was proposed using a while loop and cell arrays to store the geometries, allowing for dynamic creation of multiple boxes without manually defining each variable. The final script utilizes a while loop to draw rectangles based on specified dimensions and positions.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of COMSOL Multiphysics geometry creation
  • Knowledge of cell arrays in MATLAB
  • Basic geometry concepts related to rectangles
NEXT STEPS
  • Explore MATLAB cell arrays for dynamic data storage
  • Learn about COMSOL's geometry functions and their parameters
  • Investigate MATLAB loops and control structures
  • Review examples of integrating MATLAB with COMSOL for geometry creation
USEFUL FOR

This discussion is beneficial for MATLAB users, COMSOL Multiphysics practitioners, and engineers looking to automate geometry creation in simulations.

luhao
Messages
1
Reaction score
0
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
and i had

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
 
Physics news on Phys.org
I also had the same problems with making a loop of my geometries in comsol throug matlab. With a little help from this link:

https://www.physicsforums.com/showthread.php?t=297843

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 dimension. Then you use acces the structueres with draw{i} where i is the i'th number in your loop..

Hope it helps you:)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
11K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K