Matlab bridge extension - Module execution error

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)

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

​Do you know how can I fix it?​

​Thank you very much, :relaxed:

King regards,​

Marina C.

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?

1 Like

Thank you for your quick response Andras :slight_smile:

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;
S = size(M);
z = S(3);
L = [];

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

N = [];

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

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

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

%nonz = find(img);

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

BW2 = bwmorph(closeBW,‘remove’);
[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;
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;
M(:,:, l)=multiplica;


img.pixelData = M;

%nrrdwrite(inputParams.outputvolume, img);

cli_imagewrite(inputParams.outputvolume, img);

Thank you in advance,

Marina C.

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.

1 Like

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.


2 posts were split to a new topic: Relationship between pixels and mm from Matlab