In an assignment A(I) = B, the number of elements in B and I must be the same
Show older comments
Dear all,
I am relatively new to MATLAB and trying to work on this code to perform an intensity weighted length measurement on some fluorescence images. My code is here:
%%Load the multi-image
clear
[name, path] = uigetfile('.tif');
id =strcat(path,name);
info = imfinfo(id);
pixsiz=0.16; % to fill in
num_images = numel(info);
filename='name';
MAT = imread(id); %multi image import:
Md = double(MAT);
[l,w]=size(Md);
M2 = 1:w;
M3 = pixsiz*(1:l-2);
%imshow(Md, []);
%%Process image
% Threshold
[by,bx]=hist(Md(:),60); %h40,ls=10
ft = fittype('gauss1');
gfit = fit(bx',by',ft);
coe=coeffvalues(gfit); a1=coe(1); b1=coe(2); c1=coe(3);
bg2=(b1+0.4*c1); %h40,ls=10
Mbw1=Md>=bg2; % bw=image>=threshold;
Mbw2 = Mbw1.*Md;
%imshow(Mbw2, []);
% Errase noises by labeling connected group
[LL,num] = bwlabel(Mbw2, 8);
value = zeros([1,num]);
for ii=1: num
temp = LL == ii;
value(ii) = sum(temp(:));
end
[~,index] = max(value);
Mbw11=LL==index;
%figure, RGB = label2rgb(LL); imshow(RGB);
Mbw3 = Mbw11.*Mbw2; %image2 = bw*image;
%imshow(Mbw3, []);
%%fitting each array of image- 2
%st= A(1,1,i)/0.16; %d40 or shorter ls
b4=ones(1,l);
Ar=ones(1,l);
for k = 1:l
M1 = Mbw2(k,:);
%M3 = smooth(M1);
%plot(M2,M1,'.');
options = fitoptions('gauss1');
%options.Upper = [Inf 100 15]; options.Lower = [10 5 0]; options.StartPoint=[8000 st 5]; %d40 or shorter ls
ft = fittype('gauss1');
gfit = fit(M2',M1',ft, options);
coe=coeffvalues(gfit); a2=coe(1); b2=coe(2); c2=coe(3);
y=a2*exp(-((M2-b2)/c2).^2);
hold on; plot(M2,y,'r');
Ar(k) = sqrt(pi)*a2*c2; % Area
b4(k) = b2; %peak position
end
%%Intensity weighting factor:
%Lnanoweighting;
Ar3=Ar(Ar>0);
Ar33 = Ar3(1:end-1);
% For low percentage of coil on DNA
[hx,hy]=hist(Ar33,30); %A1.histogram of intensity values
[mx, col]=max(hx);% A2.most probability values of intensity to be a base value
Intnew=Ar33/hy(col); % A3.Intensity weighting factor (For low percentage of coil on DNA).
% For high percentage of protein_coil on DNA
%Intnew=Ar33 / min(Ar33);% Intensity weighting factor (For high
%percentage of protein_coil on DNA).
% Distance of each pixal (d)
AA=Ar>0;
b32=b4.*AA;
b33=b32(b32>0);
l2=length(b33);
d=ones(1,l2-1);
B=ones(l2,2); B(:,2)=pixsiz*b33; B(:,1)=pixsiz*(1:l2);
for p=1:l2-1;
d(p) =sqrt((B(p+1,1) - B(p,1))^2+(B(p+1,2)-B(p,2))^2);
end
L = sum(d);
Lnew = L*Intnew';
%%Print results- Lnano, Lnanoweighting
% save the value (area sizes)
B=ones(2,1); B(2)=L; B(1)=Lnew;
%outputTXT = regexprep(id, '.dat', ' - Lweighting.txt');
save([id '.dat'], 'B','-ASCII')
But, when I run this code, I see the following error message:
In an assignment A(I) = B, the number of elements in B and I must be the
same.
Error in file (line 95)
B=ones(2,1); B(2)=L; B(1)=Lnew;
Can someone please help me know the source of this problem and a possible solution?
Thanks,
1 Comment
Thomas Koelen
on 8 May 2015
Could you show me your what you get when you put
L
and
Lnew
in the command window?
Thomas
Answers (1)
The error message is fairly clear. You're trying to assign a matrix / or vector (either L, Lnew or both) to a single element of a matrix. This obviously can't work.
Unfortunately, it's fairly difficult to follow your code, not helped by the lack of comments and very non-descriptive variable names. My advice for the future is to use better variable names. For example, RawImage is much clearer than Md, so is ImageHeight, ImageWidth a lot clearer than l and w.
Anyway, the best way for you to solve the problem is to use the debugger. Place a breakpoint in your code and step through it checking the size of variables is what you expect.
Finally, while that will not make a difference for your problem, I would:
- replace strcat by fullfile when building paths.
- replace length by numel for vectors, or size with a explicit dimension for matrices. length on a 2D matrix can either be the number of columns or rows depending on which is greater, so it's never obvious from the code which dimension it returns.
- add a lot more comments to the code.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!