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)
response=evalc(cmd);

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

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 Likes

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