# Matlab Bridge Extension

**URL:** <https://discourse.slicer.org/t/matlab-bridge-extension/314>\
**Category:** Development\
**Created:** [May 15, 2017, 6:48pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314 "2017-05-15T18:48:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [May 15, 2017, 6:48pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/1 "2017-05-15T18:48:06Z")

</div>

Operating system: ubuntu 16.04  
Slicer version: 4.6 | 4.7  
Expected behavior: Matlab bridge executes Matlab properly  
Actual behavior: There is an error calling Matlab

Hi everyone!

I am trying to call a script from Matlab through MatlabBridge extension. I generate my module with _matlab module generator_ from Developer tools category. My module is perfectly created.  
When I restart slic3r to edit my module and execute it, it starts working and Matlab calling appears but disappears after few seconds; process is completed with errors. When I open the log, the following error appears:  
\_Failed to connect to server 127.0.0.1:4100

Waiting for Matlab startup … 59sec  
ERROR: Cannot connect to the server

VolumeAdj completed with errors\_

Do you know why this could happen, if I am doing something wrong?

I look forward to hearing from you. Thanks in advance.

Sincerely,

Alba.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 15, 2017, 8:14pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/2 "2017-05-15T20:14:23Z")

</div>

See [troubleshooting instructions](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/MatlabBridge#Troubleshooting) on the module’s webpage. Specifically:

- Check if firewall settings enable Matlab to open a server port 4100
- Run MatlabServer from the Matlab GUI as described in “_I would like to add breakpoints and do step-by-step debugging in Matlab_” and check if you receive incoming requests and if any error messages are displayed

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [May 23, 2017, 6:42pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/3 "2017-05-23T18:42:03Z")

</div>

Thank you!

I have followed the steps, but I still cannot run matlabbridge extension.  
I have generated a module which I called Module\_ex that executes the thresholding example. I have downloaded the MRHead sample data.  
I have checked that my firewall is not blocking the connection with port 4100:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/8/18fbf5f33845b5a4bccaac59237e97af7c701cf4.png)

![](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/1X/0154d8a907f9be78fd8b3fcc20da1c73a80bc057.png)

But I still receive the same error I showed in the first post.

After this, I have executed the debug mode following the troubleshooting instructions and I obtain this messages:

```
>> cli_commandserver
Starting OpenIGTLink command server at port 4100
Waiting for client connections...
Client connected
 Execute command: cd('/home/alba/.config/NA-MIC/Extensions-25516/MatlabModules'); cli_argswrite('/tmp/Slicer/6792_F0fE1FtcRD.params',Module_ex(cli_argsread({'--returnparameterfile','/tmp/Slicer/6792_F0fE1FtcRD.params','--threshold','50','--inputvolume','/tmp/Slicer/GHJC_vtkMRMLScalarVolumeNodeB.nrrd','--outputvolume','/tmp/Slicer/GHJC_vtkMRMLScalarVolumeNodeC.nrrd'})));
 Response (sent to device ACK): ERROR: Command execution failed. Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in nrrdwrite (line 59)
fprintf(fid,'NRRD0005\n');

Error in cli_imagewrite (line 7)
nrrdwrite(outputFilename, img);

Error in Module_ex (line 20)
cli_imagewrite(inputParams.outputvolume, img);

Error in cli_commandserver (line 91)
                response=evalc(cmd);

```

It says I have an invalid file identifier but I do not understand where or how I should generate a valid one.

Thanks in advance!  
Regards ^  
Alba : )

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 23, 2017, 7:22pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/4 "2017-05-23T19:22:22Z")

</div>

The error occurs when the MatlabBridge tries to write the output volume to /tmp/Slicer/GHJC\_vtkMRMLScalarVolumeNodeC.nrrd because of permissions, disk space, or some other issue.

See for example this page for a few ideas: [https://stackoverflow.com/questions/10606373/what-causes-an-invalid-file-identifier-in-matlab](https://stackoverflow.com/questions/10606373/what-causes-an-invalid-file-identifier-in-matlab)

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [August 3, 2017, 7:22pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/5 "2017-08-03T19:22:51Z")

</div>

Good evening,

I have been working on that matlab-slicer issue, I have research in the stackoverflow link and on my own, but I have not been able to solve it; every folde has all kind of permissions (r w x), matlab and slicer are run as an administrator (root user), and I don not know how to change the path for output data for this extension.

However, I have only reproduced this error on ubuntu, and although I would like to solve it I am working now with iOS.

Thank you so much for all! : )  
Regards,  
Alba

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 3, 2017, 7:40pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/6 "2017-08-03T19:40:37Z")

</div>

Probably your temporary folder is read-only ([https://issues.slicer.org/view.php?id=4340](https://issues.slicer.org/view.php?id=4340)). The workaround is to go to menu: Edit / Application settings / Modules, and then select a writeable directory as `Temporary directory`.

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [August 8, 2017, 8:15pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/7 "2017-08-08T20:15:53Z")

</div>

Good evening,

Thank you so so much!!, really. It finally worked when I changed the tmp directory, but I do not understand why it did not work on the default tmp path even though it had all permissions.

Now I am calling my real code with the extension. I have a function called outliers\_step1.m which is located in the MatlabModules path, and then I have my module with the .m file which executes some code lines and then calls the function, but when I run commandserver with breakpoints it shows me this error:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/8/287ec24f8a44ec2345472c74859f03ec5b976d91.png)

This is my module .m file:

 ![j](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/a/4a3afec8a100432e14960048cabddc59d392c3d2.png)

Thanks in advance,  
Have a nice day,  
Alba

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 4, 2017, 7:51pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/8 "2017-09-04T19:51:01Z")

</div>

Good evening,

I still obtain the same error, and I do not understand why it is saying that fragment of code is a function because the function is outliers\_step1.m. Do you know if I should place the function in another path? It is actually located in the MatlabModules folder with other functions from Nifti toolbox which I use and does not cause any trouble.

This is my function code:

function [outlierst1\_ut,outlierst1\_br1,outlierst1\_br2,outlierst1\_lv1,outlierst1\_lv2,outlierst1\_pl]=outliers\_s1(detJgw,plmask83,lv1mask83,lv2mask83,br1mask83,br2mask83,uterusB3,tlpl,thpl,tllv,thlv,tlbr,thbr)  
%for twin data: detJgw is the determinant of jacobian matrix;  
%lv1mask,lv2mask,br1mask,br2mask,uterusmask are the masks for livers,  
%brains and uterus;tlpl,thpl,tllv,thlv,tlbr,thbr are the thresholds for  
%over compression and over expansion for placenta, liver, and brain  
%outlierst1\_\* are the volumes after outlier rejection  
%%%function [gwuter,gwuter\_0,gwbr1,gwbr1\_0,gwbr2,gwbr2\_0,gwlv1,gwlv1\_0,gwlv2,gwlv2\_0,gwpl,gwpl\_0]=outliers\_step1(detJgw,plmask,lv1mask,lv2mask,br1mask,br2mask,uterusmask,tlpl,thpl,tllv,thlv,tlbr,thbr)  
[x,y,z,t]=size(detJgw);  
edjgw\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\<0 count0=count0+1;end,end,edjgw\_0(a,d)=count0;end,end,end  
edjgw\_tlpl=zeros(t,z);for a=1:t for d=1:z counttlpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\<tlpl && double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\>0 counttlpl=counttlpl+1;end,end,edjgw\_tlpl(a,d)=counttlpl;end,end,end  
edjgw\_thpl=zeros(t,z);for a=1:t for d=1:z countthpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\>thpl countthpl=countthpl+1;end,end,edjgw\_thpl(a,d)=countthpl;end,end,end  
for a=1:t gwuter(a)=sum(edjgw\_thpl(a,:)+edjgw\_tlpl(a,:)+edjgw\_0(a,:));end  
%for a=1:t gwuter\_0(a)=sum(edjgw\_0(a,:));end  
outlierst1\_ut=find(gwuter==0);

br1s(:,:,:)=smooth3(br1mask83(:,:,:),‘box’,7); %evaluate a bigger region  
for b=1:x for c=1:y for d=1:z if br1s(b,c,d)\>0 br1cs(b,c,d)=1; else br1cs(b,c,d)=0; end,end,end,end  
edjgwbr1\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\<0 count0=count0+1;end,end,edjgwbr1\_0(a,d)=count0;end,end,end  
edjgwbr1\_tlbr=zeros(t,z);for a=1:t for d=1:z counttlbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\<tlbr && double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\>0 counttlbr=counttlbr+1;end,end,edjgwbr1\_tlbr(a,d)=counttlbr;end,end,end  
edjgwbr1\_thbr=zeros(t,z);for a=1:t for d=1:z countthbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\>thbr countthbr=countthbr+1;end,end,edjgwbr1\_thbr(a,d)=countthbr;end,end,end  
for a=1:t gwbr1(a)=sum(edjgwbr1\_thbr(a,:)+edjgwbr1\_tlbr(a,:)+edjgwbr1\_0(a,:));end  
%for a=1:t gwbr1\_0(a)=sum(edjgwbr1\_0(a,:));end  
outlierst1\_br1=find(gwbr1==0);

br2s(:,:,:)=smooth3(br2mask83(:,:,:),‘box’,7);  
for b=1:x for c=1:y for d=1:z if br2s(b,c,d)\>0 br2cs(b,c,d)=1; else br2cs(b,c,d)=0; end,end,end,end  
edjgwbr2\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\<0 count0=count0+1;end,end,edjgwbr2\_0(a,d)=count0;end,end,end  
edjgwbr2\_tlbr=zeros(t,z);for a=1:t for d=1:z counttlbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\<tlbr && double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\>0 counttlbr=counttlbr+1;end,end,edjgwbr2\_tlbr(a,d)=counttlbr;end,end,end  
edjgwbr2\_thbr=zeros(t,z);for a=1:t for d=1:z countthbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\>thbr countthbr=countthbr+1;end,end,edjgwbr2\_thbr(a,d)=countthbr;end,end,end  
for a=1:t gwbr2(a)=sum(edjgwbr2\_thbr(a,:)+edjgwbr2\_tlbr(a,:)+edjgwbr2\_0(a,:));end  
%for a=1:t gwbr2\_0(a)=sum(edjgwbr2\_0(a,:));end  
outlierst1\_br2=find(gwbr2==0);

lv1s(:,:,:)=smooth3(lv1mask83(:,:,:),‘box’,9);  
for b=1:x for c=1:y for d=1:z if lv1s(b,c,d)\>0 lv1cs(b,c,d)=1; else lv1cs(b,c,d)=0; end,end,end,end  
edjgwlv1\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\<0 count0=count0+1;end,end,edjgwlv1\_0(a,d)=count0;end,end,end  
edjgwlv1\_tllv=zeros(t,z);for a=1:t for d=1:z counttllv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\<tllv && double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\>0 counttllv=counttllv+1;end,end,edjgwlv1\_tllv(a,d)=counttllv;end,end,end  
edjgwlv1\_thlv=zeros(t,z);for a=1:t for d=1:z countthlv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\>thlv countthlv=countthlv+1;end,end,edjgwlv1\_thlv(a,d)=countthlv;end,end,end  
for a=1:t gwlv1(a)=sum(edjgwlv1\_thlv(a,:)+edjgwlv1\_tllv(a,:)+edjgwlv1\_0(a,:));end  
%for a=1:t gwlv1\_0(a)=sum(edjgwlv1\_0(a,:));end  
outlierst1\_lv1=find(gwlv1==0);

lv2s(:,:,:)=smooth3(lv2mask83(:,:,:),‘box’,9);  
for b=1:x for c=1:y for d=1:z if lv2s(b,c,d)\>0 lv2cs(b,c,d)=1; else lv2cs(b,c,d)=0; end,end,end,end  
edjgwlv2\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\<0 count0=count0+1;end,end,edjgwlv2\_0(a,d)=count0;end,end,end  
edjgwlv2\_tllv=zeros(t,z);for a=1:t for d=1:z counttllv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\<tllv && double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\>0 counttllv=counttllv+1;end,end,edjgwlv2\_tllv(a,d)=counttllv;end,end,end  
edjgwlv2\_thlv=zeros(t,z);for a=1:t for d=1:z countthlv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\>thlv countthlv=countthlv+1;end,end,edjgwlv2\_thlv(a,d)=countthlv;end,end,end  
for a=1:t gwlv2(a)=sum(edjgwlv2\_thlv(a,:)+edjgwlv2\_tllv(a,:)+edjgwlv2\_0(a,:));end  
%for a=1:t gwlv2\_0(a)=sum(edjgwlv2\_0(a,:));end  
outlierst1\_lv2=find(gwlv2==0);

pls(:,:,:)=smooth3(plmask83(:,:,:),‘box’,9);  
for b=1:x for c=1:y for d=1:z if pls(b,c,d)\>0 plcs(b,c,d)=1; else plcs(b,c,d)=0; end,end,end,end  
edjgwpl\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\<0 count0=count0+1;end,end,edjgwpl\_0(a,d)=count0;end,end,end  
edjgwpl\_tlpl=zeros(t,z);for a=1:t for d=1:z counttlpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\<tlpl && double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\>0 counttlpl=counttlpl+1;end,end,edjgwpl\_tlpl(a,d)=counttlpl;end,end,end  
edjgwpl\_thpl=zeros(t,z);for a=1:t for d=1:z countthpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\>thpl countthpl=countthpl+1;end,end,edjgwpl\_thpl(a,d)=countthpl;end,end,end  
for a=1:t gwpl(a)=sum(edjgwpl\_thpl(a,:)+edjgwpl\_tlpl(a,:)+edjgwpl\_0(a,:));end  
%for a=1:t gwpl\_0(a)=sum(edjgwpl\_0(a,:));end  
outlierst1\_pl=find(gwpl==0);

Thank you so much,  
Have a nice day,  
Alba!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 6, 2017, 3:47am UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/9 "2017-09-06T03:47:06Z")

</div>

The issue is most likely that your filename is `outliers_step1.m` and the function name is `outliers_s1`.

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 7, 2017, 8:49pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/10 "2017-09-07T20:49:00Z")

</div>

Do you mean that they have similar names? or that an m is missing? no m is missing, function’s name is outliers\_s1.m and filename is outliers\_step1.m

Thank you!

Alba

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 8, 2017, 1:31pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/11 "2017-09-08T13:31:50Z")

</div>

Matlab function name must match the .m filename.

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 9, 2017, 11:52am UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/12 "2017-09-09T11:52:31Z")

</div>

Oh, I understand now. They match their names, see:

This is my script who calls the function: outliers\_s1.m

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/4/04aaa7634f08a6bc3c922996abc12213b0553764.png)

And this is the function (outliers\_s1.m), you can see the .m file match the function’s name:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/5/45b3b6cdebc2a484b5b039826e3f5de0849ca46e.png)

I still obtain the error my script Outliers\_step1.m is being called as a function:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/8/f83a74369440079c4b2118fc8c5f47aab58d6a7b.png)

I believe script’s name and function’s name do not have to match, do they?

Thank you so much  
Alba

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 9, 2017, 12:04pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/13 "2017-09-09T12:04:17Z")

</div>

I don’t see the complete error message. What is the rest of the message after “…cli\_argswrite(’/usr/local/bin/Slicer/MATLAB…”?

I suspect that your Slicer Matlab module name is Outliers\_step1, which must be a function (because it gets input parameters as function arguments and may provide results in return value).

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 11, 2017, 9:17pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/14 "2017-09-11T21:17:26Z")

</div>

Hi!

This is the full message (I cleaned some useless data):

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/9/89a254ccb965315d013d06d8e56e7b37a11cdc53.png)

Yes, but I define the function in a script and then I call this function which returns several parameters in another script, so the function is defined in outliers\_s1.m and I call it after loading some data in the outliers\_step1.m script. Is that not correct then?

Thank you!  
Alba

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 11, 2017, 9:26pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/15 "2017-09-11T21:26:00Z")

</div>

> [@Alba\_Garcia\_de\_la\_Ca](#):
>
> Is that not correct then?

It is not. The function definition in the file that is created by the MatlabBridge’s module generator must not be changed, but you can call any other functions or scripts from that function.

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 12, 2017, 8:46pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/16 "2017-09-12T20:46:58Z")

</div>

HI!,

Ok. I was performing that way because in Matlab I can run it that way, I had the same structure, the outliers\_s1.m function and the Outliers\_step1.m script who called outliers\_s1. function (I did that because my Matlab 2014b was giving me trouble).

Now I have implemented the code all in one, this way:

function [outlierst1\_ut,outlierst1\_br1,outlierst1\_br2,outlierst1\_lv1,outlierst1\_lv2,outlierst1\_pl]=Outliers\_step1(detJgw,plmask83,lv1mask83,lv2mask83,br1mask83,br2mask83,uterusB3,tlpl,thpl,tllv,thlv,tlbr,thbr)

detJgw=zeros(110,110,80,82);  
tlpl = 0.5; thpl=1.5; tllv=0.7; thlv=1.3; tlbr=0.8; thbr=1.2;  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/plmask83.nii’));plmask83=ans.img;  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/lv1mask83.nii’));lv1mask83=ans.img;  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/lv2mask83.nii’));lv2mask83=ans.img;  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/br1mask83.nii’));br1mask83=ans.img;  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/br2mask83.nii’));br2mask83=ans.img;  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/uterus\_B3.nii’));uterusB3=ans.img;

for a=4:6  
load\_nii(sprintf(‘/usr/local/bin/Slicer/elastix\_/oddevenB01/tmeosh\_%d/spatialJacobian.nii’,a));detJgw(:,:,2:2:80,a)=ans.img;  
load\_nii(sprintf(‘/usr/local/bin/Slicer/elastix\_/oddevenB01/tmoosh\_%d/spatialJacobian.nii’,a));detJgw(:,:,1:2:79,a)=ans.img;  
end  
%for twin data: detJgw is the determinant of jacobian matrix;  
%lv1mask,lv2mask,br1mask,br2mask,uterusmask are the masks for livers,  
%brains and uterus;tlpl,thpl,tllv,thlv,tlbr,thbr are the thresholds for  
%over compression and over expansion for placenta, liver, and brain  
%outlierst1\_\* are the volumes after outlier rejection  
%%%function [gwuter,gwuter\_0,gwbr1,gwbr1\_0,gwbr2,gwbr2\_0,gwlv1,gwlv1\_0,gwlv2,gwlv2\_0,gwpl,gwpl\_0]=outliers\_step1(detJgw,plmask,lv1mask,lv2mask,br1mask,br2mask,uterusmask,tlpl,thpl,tllv,thlv,tlbr,thbr)  
[x,y,z,t]=size(detJgw);  
edjgw\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\<0 count0=count0+1;end,end,edjgw\_0(a,d)=count0;end,end,end  
edjgw\_tlpl=zeros(t,z);for a=1:t for d=1:z counttlpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\<tlpl && double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\>0 counttlpl=counttlpl+1;end,end,edjgw\_tlpl(a,d)=counttlpl;end,end,end  
edjgw\_thpl=zeros(t,z);for a=1:t for d=1:z countthpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(uterusB3(b,c,d))\>thpl countthpl=countthpl+1;end,end,edjgw\_thpl(a,d)=countthpl;end,end,end  
for a=1:t gwuter(a)=sum(edjgw\_thpl(a,:)+edjgw\_tlpl(a,:)+edjgw\_0(a,:));end  
%for a=1:t gwuter\_0(a)=sum(edjgw\_0(a,:));end  
outlierst1\_ut=find(gwuter==0);

br1s(:,:,:)=smooth3(br1mask83(:,:,:),‘box’,7); %evaluate a bigger region  
for b=1:x for c=1:y for d=1:z if br1s(b,c,d)\>0 br1cs(b,c,d)=1; else br1cs(b,c,d)=0; end,end,end,end  
edjgwbr1\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\<0 count0=count0+1;end,end,edjgwbr1\_0(a,d)=count0;end,end,end  
edjgwbr1\_tlbr=zeros(t,z);for a=1:t for d=1:z counttlbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\<tlbr && double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\>0 counttlbr=counttlbr+1;end,end,edjgwbr1\_tlbr(a,d)=counttlbr;end,end,end  
edjgwbr1\_thbr=zeros(t,z);for a=1:t for d=1:z countthbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br1cs(b,c,d))\>thbr countthbr=countthbr+1;end,end,edjgwbr1\_thbr(a,d)=countthbr;end,end,end  
for a=1:t gwbr1(a)=sum(edjgwbr1\_thbr(a,:)+edjgwbr1\_tlbr(a,:)+edjgwbr1\_0(a,:));end  
%for a=1:t gwbr1\_0(a)=sum(edjgwbr1\_0(a,:));end  
outlierst1\_br1=find(gwbr1==0);

br2s(:,:,:)=smooth3(br2mask83(:,:,:),‘box’,7);  
for b=1:x for c=1:y for d=1:z if br2s(b,c,d)\>0 br2cs(b,c,d)=1; else br2cs(b,c,d)=0; end,end,end,end  
edjgwbr2\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\<0 count0=count0+1;end,end,edjgwbr2\_0(a,d)=count0;end,end,end  
edjgwbr2\_tlbr=zeros(t,z);for a=1:t for d=1:z counttlbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\<tlbr && double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\>0 counttlbr=counttlbr+1;end,end,edjgwbr2\_tlbr(a,d)=counttlbr;end,end,end  
edjgwbr2\_thbr=zeros(t,z);for a=1:t for d=1:z countthbr=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(br2cs(b,c,d))\>thbr countthbr=countthbr+1;end,end,edjgwbr2\_thbr(a,d)=countthbr;end,end,end  
for a=1:t gwbr2(a)=sum(edjgwbr2\_thbr(a,:)+edjgwbr2\_tlbr(a,:)+edjgwbr2\_0(a,:));end  
%for a=1:t gwbr2\_0(a)=sum(edjgwbr2\_0(a,:));end  
outlierst1\_br2=find(gwbr2==0);

lv1s(:,:,:)=smooth3(lv1mask83(:,:,:),‘box’,9);  
for b=1:x for c=1:y for d=1:z if lv1s(b,c,d)\>0 lv1cs(b,c,d)=1; else lv1cs(b,c,d)=0; end,end,end,end  
edjgwlv1\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\<0 count0=count0+1;end,end,edjgwlv1\_0(a,d)=count0;end,end,end  
edjgwlv1\_tllv=zeros(t,z);for a=1:t for d=1:z counttllv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\<tllv && double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\>0 counttllv=counttllv+1;end,end,edjgwlv1\_tllv(a,d)=counttllv;end,end,end  
edjgwlv1\_thlv=zeros(t,z);for a=1:t for d=1:z countthlv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv1cs(b,c,d))\>thlv countthlv=countthlv+1;end,end,edjgwlv1\_thlv(a,d)=countthlv;end,end,end  
for a=1:t gwlv1(a)=sum(edjgwlv1\_thlv(a,:)+edjgwlv1\_tllv(a,:)+edjgwlv1\_0(a,:));end  
%for a=1:t gwlv1\_0(a)=sum(edjgwlv1\_0(a,:));end  
outlierst1\_lv1=find(gwlv1==0);

lv2s(:,:,:)=smooth3(lv2mask83(:,:,:),‘box’,9);  
for b=1:x for c=1:y for d=1:z if lv2s(b,c,d)\>0 lv2cs(b,c,d)=1; else lv2cs(b,c,d)=0; end,end,end,end  
edjgwlv2\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\<0 count0=count0+1;end,end,edjgwlv2\_0(a,d)=count0;end,end,end  
edjgwlv2\_tllv=zeros(t,z);for a=1:t for d=1:z counttllv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\<tllv && double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\>0 counttllv=counttllv+1;end,end,edjgwlv2\_tllv(a,d)=counttllv;end,end,end  
edjgwlv2\_thlv=zeros(t,z);for a=1:t for d=1:z countthlv=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(lv2cs(b,c,d))\>thlv countthlv=countthlv+1;end,end,edjgwlv2\_thlv(a,d)=countthlv;end,end,end  
for a=1:t gwlv2(a)=sum(edjgwlv2\_thlv(a,:)+edjgwlv2\_tllv(a,:)+edjgwlv2\_0(a,:));end  
%for a=1:t gwlv2\_0(a)=sum(edjgwlv2\_0(a,:));end  
outlierst1\_lv2=find(gwlv2==0);

pls(:,:,:)=smooth3(plmask83(:,:,:),‘box’,9);  
for b=1:x for c=1:y for d=1:z if pls(b,c,d)\>0 plcs(b,c,d)=1; else plcs(b,c,d)=0; end,end,end,end  
edjgwpl\_0=zeros(t,z);for a=1:t for d=1:z count0=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\<0 count0=count0+1;end,end,edjgwpl\_0(a,d)=count0;end,end,end  
edjgwpl\_tlpl=zeros(t,z);for a=1:t for d=1:z counttlpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\<tlpl && double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\>0 counttlpl=counttlpl+1;end,end,edjgwpl\_tlpl(a,d)=counttlpl;end,end,end  
edjgwpl\_thpl=zeros(t,z);for a=1:t for d=1:z countthpl=0; for b=1:x for c=1:y if double(detJgw(b,c,d,a)).\*double(plcs(b,c,d))\>thpl countthpl=countthpl+1;end,end,edjgwpl\_thpl(a,d)=countthpl;end,end,end  
for a=1:t gwpl(a)=sum(edjgwpl\_thpl(a,:)+edjgwpl\_tlpl(a,:)+edjgwpl\_0(a,:));end  
%for a=1:t gwpl\_0(a)=sum(edjgwpl\_0(a,:));end  
outlierst1\_pl=find(gwpl==0);

It is the same, but loading parameters in the function. Now, while debugging I do not obtain an error, but the script stays running without stop so it is not ok. When I stop the module in slicer I obtain this error:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/7/87cd04da5f1f48a2b07eb5ff1316df88bd205f84.png)

But I guess it is caused due to the interruption

Do you know what could be happening?

Thank you so much,  
Alba

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 13, 2017, 9:17pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/17 "2017-09-13T21:17:59Z")

</div>

Hi again!

Sorry if I am doing so many questions, or if they are simple or easy. Calling this function and the following is the last step to end my final degree.

Now I call another function, but it says an input parameter is referenced and not found, but it is not the case:

Function Outliers\_step2.m:

%%function [outlierst2,regmask\_or]=outlier\_step2(regdata,refframe,mask,outlierst1)  
function [outlierst2,regmask,outlier,smoothmask]=outlier\_step2(regdata,refframe,mask,outlierst1)  
%Input  
%regdata: registered data set, refframe: reference frame number, mask: used  
for a=1:5  
load\_untouch\_nii(sprintf(‘/usr/local/bin/Slicer/elastix\_/nonrigid\_%d/result.0.nii’,a));regdata(:,:,:,a)=ans.img;  
end  
refframe= 31;  
%mask to calculate the average signal, outlierst1: at the end, to exclude the ones  
load\_untouch\_nii(sprintf(‘012115/uterus\_B3.nii’));mask=ans.img;  
outlierst1 = load(sprintf(‘/usr/local/bin/Slicer/outlierst1\_ut.mat’))  
%rejected due to over deformation.  
%Output  
%outlierst2 are the volumes after outlier rejection  
%regmask: updated mask showing corrected region with 1 and outlier voxels  
%with 2,outlier: number of outlier voxels for each time frame,  
%smoothmask:original mask  
[x,y,z,t]=size(regdata);  
%smoothmask=zeros(x,y,z);  
%smask(:,:,:)=smooth3(mask(:,:,:),‘box’,3);  
%for b=1:x for c=1:y for d=1:z if smask(b,c,d)\>0.5 smoothmask(b,c,d)=1; else smoothmask(b,c,d)=0; end,end,end,end  
smoothmask=mask(:,:,:);

regmask=zeros(x,y,z,t);  
regmasked=zeros(x,y,z,t);  
for k=1:t  
regmask(:,:,:,k)=smoothmask(:,:,:);  
regmasked(:,:,:,k)=double(smoothmask(:,:,:)).\*double(regdata(:,:,:,k));  
end  
Mref=regmasked(:,:,:,refframe);  
%med=sort(nonzeros(Mref(:)));  
%Qmid=floor(nnz(med)/2)+1;  
%Qmidv=med(Qmid);  
mb=mean(nonzeros(Mref(:)));  
sb=std(nonzeros(Mref(:)));  
mdummy=mb;  
%mdummy=Qmidv;  
sdummy=sb;  
outlier=zeros(1,t);  
regmaskedn=regmasked;

for k=refframe:(t-1)  
for a=1:x  
for b=1:y  
for c=1:z  
if abs(regmaskedn(a,b,c,k)-regmaskedn(a,b,c,k+1))\>mdummy  
regmask(a,b,c,k+1)=2;  
regmaskedn(a,b,c,k+1)=regmaskedn(a,b,c,k);  
outlier(k+1)=outlier(k+1)+1;  
end

```
        end
    end
end
for a=2:(x-1)
    for b=2:(y-1)
        for c=2:(z-1)
            if regmask(a-1,b+1,c,k+1)==2 && regmask(a-1,b,c,k+1)==2 && regmask(a-1,b-1,c,k+1)==2 && ...
                    regmask(a,b+1,c,k+1)==2 && regmask(a,b,c,k+1)==1 && regmask(a,b-1,c,k+1)==2 && ...
                    regmask(a+1,b+1,c,k+1)==2 && regmask(a+1,b,c,k+1)==2 && regmask(a+1,b-1,c,k+1)==2

```

% regmask(a-1,b+1,c-1,k+1)==2 && regmask(a-1,b,c-1,k+1)==2 && regmask(a-1,b-1,c-1,k+1)==2 && …  
% regmask(a,b+1,c-1,k+1)==2 && regmask(a,b,c-1,k+1)==2 && regmask(a,b-1,c-1,k+1)==2 && …  
% regmask(a+1,b+1,c-1,k+1)==2 && regmask(a+1,b,c-1,k+1)==2 && regmask(a+1,b-1,c-1,k+1)==2 && …  
% regmask(a-1,b+1,c+1,k+1)==2 && regmask(a-1,b,c+1,k+1)==2 && regmask(a-1,b-1,c+1,k+1)==2 && …  
% regmask(a,b+1,c+1,k+1)==2 && regmask(a,b,c+1,k+1)==2 && regmask(a,b-1,c+1,k+1)==2 && …  
% regmask(a+1,b+1,c+1,k+1)==2 && regmask(a+1,b,c+1,k+1)==2 && regmask(a+1,b-1,c+1,k+1)==2

```
                regmask(a,b,c,k+1)=2;
                regmaskedn(a,b,c,k+1)=regmaskedn(a,b,c,k);
                outlier(k+1)=outlier(k+1)+1;
            end
        end
    end
end
for a=2:(x-1)
    for b=2:(y-1)
        for c=2:(z-1)
            if regmask(a-1,b+1,c,k+1)==1 && regmask(a-1,b,c,k+1)==1 && regmask(a-1,b-1,c,k+1)==1 && ...
                    regmask(a,b+1,c,k+1)==1 && regmask(a,b,c,k+1)==2 && regmask(a,b-1,c,k+1)==1 && ...
                    regmask(a+1,b+1,c,k+1)==1 && regmask(a+1,b,c,k+1)==1 && regmask(a+1,b-1,c,k+1)==1

```

% regmask(a-1,b+1,c-1,k+1)==1 && regmask(a-1,b,c-1,k+1)==1 && regmask(a-1,b-1,c-1,k+1)==1 && …  
% regmask(a,b+1,c-1,k+1)==1 && regmask(a,b,c-1,k+1)==1 && regmask(a,b-1,c-1,k+1)==1 && …  
% regmask(a+1,b+1,c-1,k+1)==1 && regmask(a+1,b,c-1,k+1)==1 && regmask(a+1,b-1,c-1,k+1)==1 && …  
% regmask(a-1,b+1,c+1,k+1)==1 && regmask(a-1,b,c+1,k+1)==1 && regmask(a-1,b-1,c+1,k+1)==1 && …  
% regmask(a,b+1,c+1,k+1)==1 && regmask(a,b,c+1,k+1)==1 && regmask(a,b-1,c+1,k+1)==1 && …  
% regmask(a+1,b+1,c+1,k+1)==1 && regmask(a+1,b,c+1,k+1)==1 && regmask(a+1,b-1,c+1,k+1)==1  
regmask(a,b,c,k+1)=1;  
outlier(k+1)=outlier(k+1)-1;  
end  
end  
end  
end  
M=regmaskedn(:,:,:,k+1);  
%med=sort(nonzeros(M(:)));  
%Qmid=floor(nnz(med)/2)+1;  
%Qmidv=med(Qmid);  
%mdummy=Qmidv;  
mdummy=mean(nonzeros(M(:)));  
sdummy=std(nonzeros(M(:)));  
end

for k=refframe:-1:2  
for a=1:x  
for b=1:y  
for c=1:z  
if abs(regmaskedn(a,b,c,k)-regmaskedn(a,b,c,k-1))\>mdummy  
regmask(a,b,c,k-1)=2;  
regmaskedn(a,b,c,k-1)=regmaskedn(a,b,c,k);  
outlier(k-1)=outlier(k-1)+1;  
end

```
        end
    end
end
for a=2:(x-1)
    for b=2:(y-1)
        for c=2:(z-1)
            if regmask(a-1,b+1,c,k-1)==2 && regmask(a-1,b,c,k-1)==2 && regmask(a-1,b-1,c,k-1)==2 && ...
                    regmask(a,b+1,c,k-1)==2 && regmask(a,b,c,k-1)==1 && regmask(a,b-1,c,k-1)==2 && ...
                    regmask(a+1,b+1,c,k-1)==2 && regmask(a+1,b,c,k-1)==2 && regmask(a+1,b-1,c,k-1)==2 

```

% regmask(a-1,b+1,c-1,k-1)==2 && regmask(a-1,b,c-1,k-1)==2 && regmask(a-1,b-1,c-1,k-1)==2 && …  
% regmask(a,b+1,c-1,k-1)==2 && regmask(a,b,c-1,k-1)==2 && regmask(a,b-1,c-1,k-1)==2 && …  
% regmask(a+1,b+1,c-1,k-1)==2 && regmask(a+1,b,c-1,k-1)==2 && regmask(a+1,b-1,c-1,k-1)==2 && …  
% regmask(a-1,b+1,c+1,k-1)==2 && regmask(a-1,b,c+1,k-1)==2 && regmask(a-1,b-1,c+1,k-1)==2 && …  
% regmask(a,b+1,c+1,k-1)==2 && regmask(a,b,c+1,k-1)==2 && regmask(a,b-1,c+1,k-1)==2 && …  
% regmask(a+1,b+1,c+1,k-1)==2 && regmask(a+1,b,c+1,k-1)==2 && regmask(a+1,b-1,c+1,k-1)==2  
regmask(a,b,c,k-1)=2;  
regmaskedn(a,b,c,k-1)=regmaskedn(a,b,c,k);  
outlier(k-1)=outlier(k-1)+1;  
end  
end  
end  
end  
for a=2:(x-1)  
for b=2:(y-1)  
for c=2:(z-1)  
if regmask(a-1,b+1,c,k-1)==1 && regmask(a-1,b,c,k-1)==1 && regmask(a-1,b-1,c,k-1)==1 && …  
regmask(a,b+1,c,k-1)==1 && regmask(a,b,c,k-1)==2 && regmask(a,b-1,c,k-1)==1 && …  
regmask(a+1,b+1,c,k-1)==1 && regmask(a+1,b,c,k-1)==1 && regmask(a+1,b-1,c,k-1)==1  
% regmask(a-1,b+1,c-1,k-1)==1 && regmask(a-1,b,c-1,k-1)==1 && regmask(a-1,b-1,c-1,k-1)==1 && …  
% regmask(a,b+1,c-1,k-1)==1 && regmask(a,b,c-1,k-1)==1 && regmask(a,b-1,c-1,k-1)==1 && …  
% regmask(a+1,b+1,c-1,k-1)==1 && regmask(a+1,b,c-1,k-1)==1 && regmask(a+1,b-1,c-1,k-1)==1 && …  
% regmask(a-1,b+1,c+1,k-1)==1 && regmask(a-1,b,c+1,k-1)==1 && regmask(a-1,b-1,c+1,k-1)==1 && …  
% regmask(a,b+1,c+1,k-1)==1 && regmask(a,b,c+1,k-1)==1 && regmask(a,b-1,c+1,k-1)==1 && …  
% regmask(a+1,b+1,c+1,k-1)==1 && regmask(a+1,b,c+1,k-1)==1 && regmask(a+1,b-1,c+1,k-1)==1  
regmask(a,b,c,k-1)=1;  
outlier(k-1)=outlier(k-1)-1;  
end  
end  
end  
end  
M=regmaskedn(:,:,:,k-1);  
% med=sort(nonzeros(M(:)));  
% Qmid=floor(nnz(med)/2)+1;  
% Qmidv=med(Qmid);  
% mdummy=Qmidv;  
mdummy=mean(nonzeros(M(:)));  
sdummy=std(nonzeros(M(:)));  
end  
tv=nnz(smoothmask);  
orvols=find(outlier(outlierst1)\<tv\*5/100); %threshold %5 of voxels  
outlierst2=outlierst1(orvols);

for a=1:t for b=1:x for c=1:y for d=1:z if regmask(b,c,d,a)==0 || regmask(b,c,d,a)==2 regmask\_or(b,c,d,a)=0; else regmask\_or(b,c,d,a)=1;end,end,end,end,end

% for a=1:t regmask\_ors(:,:,:,a)=smooth3(regmask\_or(:,:,:,a),‘box’,3);end  
% for a=1:t for b=1:x for c=1:y for d=1:z if regmask\_ors(b,c,d,a)\>0.5 regmask\_orsc(b,c,d,a)=1; else regmask\_orsc(b,c,d,a)=0; end,end,end,end,end

And the xml from matlabbridge for that module is the following:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/c/ac4fc39a7cf311b64db9e3560c27e9d758373270.png)

Do you know waht could be happening?

Thank you so much,  
Alba

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 13, 2017, 9:45pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/18 "2017-09-13T21:45:43Z")

</div>

It’s very difficult to browse and understand the long code files that you include in your message. Could you please upload all relevant files to a GitHub repository and just copy-paste the link here? Thanks!

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 14, 2017, 4:36am UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/19 "2017-09-14T04:36:25Z")

</div>

Of course!

This is the files I use in Matlab for Outliers\_step1 (they work), their names are outliers\_s1.m for the function, and out\_step1\_2.m for the script who load parameters and calls the function:

outliers\_s1.m

> <https://github.com/albagcs/Final-Degree/blob/master/outliers_s1.m>

out\_step1\_2.m

> <https://github.com/albagcs/Final-Degree/blob/master/out_step1_2.m>

And this is the one for Slicer’s module of Matlab Bridge

> <https://github.com/albagcs/Final-Degree/blob/master/Outliers_step1.m>

> <https://github.com/albagcs/Final-Degree/blob/master/Outliers_step1.xml>

The error in this module in slicer is, after putting all in the same function file, it stays running and no ends until I stop manually the module in Slicer, and then it shows me the error:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/a/0a3d74ab331214b5ad03fab22bd39cf9b65a46bc.png)

This is the code for Outliers\_step2 in Slicer’s module of Matlab Bridge

> <https://github.com/albagcs/Final-Degree/blob/master/Outliers_step2.m>

> <https://github.com/albagcs/Final-Degree/blob/master/Outliers_step2.xml>

The error for this last outliers\_step2 is the following:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/f/6f303c8f21fe19b1a633036553329a1021437feb.png)

Thank you so much,  
Alba

---

<div class="post-metadata">

**Author:** ![Alba\_Garcia\_de\_la\_Ca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alba_garcia_de_la_ca/32/181_2.png) [@Alba\_Garcia\_de\_la\_Ca](https://discourse.slicer.org/u/Alba_Garcia_de_la_Ca)\
**Post date:** [September 17, 2017, 8:34pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension/314/20 "2017-09-17T20:34:23Z")

</div>

Could have been code clearer in that way?

Thank you!  
Alba

[Next page](https://discourse.slicer.org/t/matlab-bridge-extension/314.md?page=2)
