Clear Filters
Clear Filters

How to make random voids with for loop?

1 view (last 30 days)
Hi eveyone, I have a question regarding MATLAB and COMSOL. I have a membrane in COMSOL and I wanna add voids in the MATLAB code which is linked to COMSOL. The following code gives me random locatoin as 0 and 1, which, 1 means the location of a void.
x = linspace(-2,2,10) ; y = linspace(-2,5,10) ; [X,Y] = meshgrid(x,y) ; c = randi([0 1], size(Y))==1 P = [X(c) Y(c)] figure plot(X(c),Y(c), '.r') hold on plot(X(~c), Y(~c), '.b') hold off axis('padded') legend('1','0', 'Location','best')
I need a code that can give me the number of 1's (the number of voids in the membrane) and their locations.
Then, I know how write the code for one, two voids and so on in MATLAB linked to COMSOL. However, adding them by hands is very time consuming. The code is as follows:
model.component('comp1').geom('geom1').feature('wp1').geom.create('e1', 'Ellipse'); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e1').set('semiaxes', {'R/10' 'R/15'}); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e1').set('pos', {'0' '0'}); model.component('comp1').geom('geom1').feature('wp1').geom.create('e2', 'Ellipse'); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e2').set('semiaxes', {'R/15' 'R/15'}); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e2').set('pos', {'1.5e-4' '0'}); model.component('comp1').geom('geom1').feature('wp1').geom.create('dif1', 'Difference'); model.component('comp1').geom('geom1').feature('wp1').geom.feature('dif1').selection('input').set({'r1'}); model.component('comp1').geom('geom1').feature('wp1').geom.feature('dif1').selection('input2').set({'e1' 'e2'}); model.component('comp1').geom('geom1').run('wp1'); model.component('comp1').geom('geom1').run('fin');
In this code 'comp1' , 'geom1', 'wp1', 'dif1', selection('input') and selection('input2') aren't going to change. In a nutshel, How can I write a code which can generate a new membrane with random voids? More details: For instance if I have 10 voids, then I have e1 to e10 and for each one I should add their location in .set('pos', {'0' '0'}). Also I should add e1 to e 10 in selection('input2').set({'e1' 'e2'}).
Thank you all in advance.

Accepted Answer

Stephen23
Stephen23 on 23 Feb 2023
Edited: Stephen23 on 23 Feb 2023
Why not just use a loop, something like this (aircode):
% ... other model settings
S = "e" + (1:10);
for k = 1:numel(S)
F = S{k};
model.component('comp1').geom('geom1').feature('wp1').geom.feature(F).set('semiaxes', {'R/10' 'R/15'});
model.component('comp1').geom('geom1').feature('wp1').geom.feature(F).set('pos', {'0' '0'});
end % ^
% ... more model settings
model.component('comp1').geom('geom1').feature('wp1').geom.feature('dif1').selection('input2').set(cellstr(S));
% ^^^^^^^^^^
I don't see how this is related to "How to loop through variables names?" at all. Nothing in your description is related to variable names.
  2 Comments
Mehrdad Nasirshoaibi
Mehrdad Nasirshoaibi on 23 Feb 2023
Awesome, It works perfect. Thank you sooo much.
May I ask another qeustion?
In the first part of my quesetion I wrote a code which gives me random locatoin as 0 and 1, which, 1 means the location of a void.
How can I find the number of 1's (in your code it should be instead of 10).
and how can I insert the position of each location to your code in geom.feature(F).set('pos', {'0' '0'}) instead of {'0' '0'}.
Stephen23
Stephen23 on 23 Feb 2023
"how can I insert the position of each location to your code in geom.feature(F).set('pos', {'0' '0'}) instead of {'0' '0'}."
I guess you would have to convert the required values to character and place them in a 2x1 cell array: from your code it is unclear which (pairs of) values you want to convert.
x = linspace(-2,2,10);
y = linspace(-2,5,10);
[X,Y] = meshgrid(x,y);
c = randi(0:1,size(Y))==1;
P = [X(c),Y(c)];
plot(X(c),Y(c),'.r')
hold on
plot(X(~c), Y(~c),'.b')
axis('padded')
legend('1','0', 'Location','best')

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!