# Transform Matlab script to Lumerical scripting language- example and useful tips

#1

In some cases, I cannot defined the length of the variable in advance. But, in Lumerical scripting, it always asked to defined a variable in advance with size, if the variable value is changed in for loop, while in matlab it doesn’t need. Somebody have any idea, how to convert this simple working matlab script into Lumerical script in more simple way.
test_for_hex_cell.lsf (947 Bytes)

hexagon_with_center_at_origin.m (830 Bytes)

PS: I am trying to make a hexagonal array shape, in which I need centre exactly at (0, 0). I will fixed the rods of different radius at these location, whose values are location dependent on (x,y).

#2

Dear @visvas

If you have both software installed on the same computer, the easiest way would be to use matlab command. This means that you simply need to modify your code to something like this:

script in Lumerical = matlab("script in MATLAB");

but please bare in mind that you will not get any error message in Lumerical Script File Editor neither in MATLAB if you have any errors in your script.

The output from the MATLAB commands will be printed at the Lumerical script prompt. One limitation of the matlab function is that no error reporting is provided to either the Lumerical script prompt or the MATLAB prompt. MATLAB commands should be tested by typing them directly into the MATLAB prompt before they are called from a Lumerical script. The output buffer length is roughly 1e5 characters. Additional output will be truncated.

matlabget and matlabput are useful commands to use as well.

I also modified your code to be used in Lumerical for you review:

# Clear all data

clear;

l = 6e-6;
w = 6e-6;
p = 0.8e-6;          # Periodicity of the Si disk (0.8 micron)

# Get the coordinate of hexagonal array with center x and y = 0.

if (mod(floor(l/p),2)==0){
no_points=floor(l/p)-1;
}else{
no_points=floor(l/p);
}

floor(w/(sqrt(3)/2*p));
if (mod(floor(w/(sqrt(3)/2*p)),2)==0){
no_rows=floor(w/(sqrt(3)/2*p))-1;
}else{
no_rows=floor(w/(sqrt(3)/2*p));
}

x=matrix(no_rows,no_points);
y=matrix(no_rows,no_points);

for (i=1:no_rows)
{
for (j=1:no_points)
{
x(i,j)=((no_points-1)/2-(j-1));
y(i,j)=((no_rows-1)/2-(i-1));
}
}

for (i=1:no_rows)
{
if (mod(floor(no_rows/2)+1,2)==0){
if(mod(i,2)==0){
x(i,1:no_points)=x(i,1:no_points)*p;
}
else{
x(i,1:no_points)=x(i,1:no_points)*p+p/2;
}
}
else {
if(mod(i,2)==0){
x(i,1:no_points)=x(i,1:no_points)*p+p/2*ones(1,no_points);
}
else{
x(i,1:no_points)=x(i,1:no_points)*p;
}
}
}

y=y*sqrt(3)/2*p;

#matlabput(x,y);
#matlab("plot(x,y,'*')");

V=[x,y];
set("vertices",V);
set("name","random_polygon");
set("z span",1e-6);


Since I didn’t have MATLAB on my computer I couldn’t open the .mat file or check the commands. My apologies if you find any error in my code.

Thanks

#3

@bkhanaliloo, Million thanks for this command ( script in Lumerical = matlab(“script in MATLAB”);
Also, I am happy that you modified my code and correct it. It is solved.

#4

Dear @visvas

I am glad that it worked for you. best of luck.

Thanks