Matlab Bridge Extension

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.

See troubleshooting instructions 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

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

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 : )

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

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

Probably your temporary folder is read-only (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.

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:

This is my module .m file:

Thanks in advance,
Have a nice day,
Alba

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!

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

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

Matlab function name must match the .m filename.

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

This is my script who calls the function: outliers_s1.m

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

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

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

Thank you so much
Alba

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).

Hi!

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

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

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.

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:

But I guess it is caused due to the interruption

Do you know what could be happening?

Thank you so much,
Alba

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:

Do you know waht could be happening?

Thank you so much,
Alba

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!

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

out_step1_2.m

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

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

This is the code for Outliers_step2 in Slicer’s module of Matlab Bridge

The error for this last outliers_step2 is the following:

Thank you so much,
Alba

Could have been code clearer in that way?

Thank you!
Alba