# Matlab bridge extension - Module execution error

**URL:** <https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402>\
**Category:** Development\
**Created:** [May 30, 2017, 1:39pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402 "2017-05-30T13:39:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [May 30, 2017, 1:39pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402/1 "2017-05-30T13:39:20Z")

</div>

Operating system: iOS10.3.2  
Slicer version: 4.6.2  
Expected behavior: Matlab module executes properly  
Actual behavior: “Completed with errors”

Dear team,

I’m trying to create a Matlab module for Slicer with Matlab Bridge.

I get an error when trying to write the info from a matrix M into the img.pixelData param.  
This is a part of the algorithm:

function outputParams=TFG(inputParams)

% Parameters:  
% inputParams.threshold: threshold value  
% inputParams.inputvolume: input image filename  
% inputParams.outputvolume: output image filename, result of the processing  
% outputParams.distancia: points distance

img = cli\_imageread(inputParams.inputvolume);

**M = img.pixelData;**

S = size(M);

img.pixelData = M;  
cli\_imagewrite(inputParams.outputvolume, img);

I get the error:

**expr: syntax error**  
**Failed to execute Matlab function: TFG, received the following error message:**  
**ERROR: Command execution failed. Field assignment to a non-structure array object.**

**Error in TFG (line 102)**  
**img.pixelData(1) = M;**

**Error in cli\_commandserver (line 91)**  
**response=evalc(cmd);**

**Error in run (line 86)**  
**evalin(‘caller’, [script ‘;’]);**

​Do you know how can I fix it?​

​Thank you very much, 😌

King regards,​

Marina C.

---

<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 30, 2017, 1:55pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402/2 "2017-05-30T13:55:10Z")

</div>

Probably M is processed and its type is changed. Do you modify M? In TFG function, which one is line 102? It seems that you have 20-30 lines here but Matlab complains that the error occurs in line 102.

Could you please add `whos('M')` after line `M = img.pixelData;` and also before line `img.pixelData = M;` and write me what is the output?

---

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [May 30, 2017, 4:39pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402/3 "2017-05-30T16:39:31Z")

</div>

Thank you for your quick response Andras 🙂

I’ve added those whos(‘M’) lines, but I’ve not been able to see any output.

This is the complete code:

**img = cli\_imageread(inputParams.inputvolume);**

**M = img.pixelData;**  
**whos(‘M’)**  
**S = size(M);**  
**z = S(3);**  
**L = [];**  
**p=1;**

**for i=1:z**  
\*\* if (nnz(M(:,:,i))\>1)\*\*  
\*\* L§ = i;\*\*  
\*\* p=p+1;\*\*  
\*\* end\*\*  
**end**

**N = [];**

**for i=1:p-1**  
\*\* n = nnz(M(:,:,L(i)));\*\*  
\*\* N(i, 1) = n;\*\*  
\*\* N(i, 2) = L(i);\*\*  
**end**

**[V, I] = max(N(:,1));**  
**l = N(I,2);**

**C = M(:,:, l);**  
**im = image©;**  
**img = mat2gray©;**  
**imbw = im2bw(img, 0.2);**

**%nonz = find(img);**

**%close**  
**originalBW = imbw;**  
**imshow(originalBW);**  
**se = strel(‘disk’,10);**  
**closeBW = imclose(originalBW,se);**  
**figure, imshow(closeBW)**

**BW2 = bwmorph(closeBW,‘remove’);**  
**%figure**  
**%imshow(BW2)**  
**[row,col] = find(BW2==true);**

**%codigo distancia**

x = row;  
y = col;  
count = 1;  
distance = [];  
for i=1:length(x)-1  
for j=1:length(x)  
distance(count) = sqrt((x(i) - x(j))^2 + (y(i) - y(j))^2);  
Matrix(count, : ) = [x(i) y(i) x(j) y(j) distance(count)];  
count = count +1;  
end  
end  
**SortedMatrix = sortrows(Matrix, 5);**  
**MaxDis = SortedMatrix(size(Matrix, 1), :);**  
**%fprintf(‘Coordinates of maximum distnace are (x1, y1) = (%d, %d) and (x2, y2) = (%d, %d)\n’, MaxDis(1), MaxDis(2), MaxDis(3), MaxDis(4))**  
**%fprintf(‘Maximum distance = %d\n’, MaxDis(5))**

**%Point1 = [MaxDis(1), MaxDis(2)];**  
**%Point2 = [MaxDis(3), MaxDis(4)];**

**BW2\_int = int16(BW2);**

**closeBW\_int = int16(closeBW);**

\*_multiplica = closeBW\_int.BW2\_int;_

**grupo = find(multiplica);**

**multiplica(MaxDis(1),MaxDis(2)) = 20;**  
**multiplica(MaxDis(3),MaxDis(4)) = 20;**  
**figure**  
**image(multiplica)**  
**M(:,:,:)=0;**  
**M(:,:, l)=multiplica;**  
**figure**  
**image(M(:,:,l))**

**outputParams.distancia=MaxDis(5);**

**whos(‘M’)**  
**img.pixelData = M;**

**%nrrdwrite(inputParams.outputvolume, img);**

**cli\_imagewrite(inputParams.outputvolume, img);**

Thank you in advance,

Marina C.

---

<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 30, 2017, 8:14pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402/4 "2017-05-30T20:14:34Z")

</div>

The problem is that you overwrite `img` variable in this line:

```
img = mat2gray(C);

```

Choose different name for the temporary variable and it should work well.

---

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [May 30, 2017, 9:01pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402/5 "2017-05-30T21:01:20Z")

</div>

Thank you very much for your help, it worked!

I hope to keep learning and be able to help others in the future.

Best regards,

Marina C.

---

<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:** [June 2, 2017, 4:37pm UTC](https://discourse.slicer.org/t/matlab-bridge-extension-module-execution-error/402/6 "2017-06-02T16:37:14Z")

</div>

2 posts were split to a new topic: [Relationship between pixels and mm from Matlab](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427)
