MATLAB Answers


please give correction in code

Asked by Lalit Patil on 29 Nov 2012

The image and code is

clear all;
I = imread('l.jpg');
%RGB = RGB(:,:,3);
%GRAY = rgb2gray(I);
threshold = graythresh(I);
originalImage = im2bw(I, threshold);
i = bwareaopen(originalImage,350);
m = max(max(i));
[r c] = find(i == m);
fid = fopen('lalit1.txt','wt');
for j=1:length(r)
fprintf(fid,'%f %f\n',r(j),c(j));
data = textread('lalit1.txt');
r = unique(data);
for i=r',
      c = data(data(:,1)==i,2);
      z(i,1) = mean([min(c) max(c)]);

This shows error..

??? Subscripted assignment dimension mismatch.



No products are associated with this question.

2 Answers

Answer by Andrei Bobrov
on 29 Nov 2012
Edited by Andrei Bobrov
on 29 Nov 2012

last 6 rows:

r = unique(data);
z = nan(numel(r),2);
for i1 = 1:numel(r)
    c = data(data(:,1)==r(i1),2);
    z(i1,:) = mean([min(c) max(c)]);



Yeh, now it is working, but all values of Z are Nan.. Why.?

That would happen if r was empty.

textread() is obsolete. Please recode using textscan().

Answer by Walter Roberson
on 29 Nov 2012

Where does it show that error?

If it is the z assignment in the for loop, then put a breakpoint in at the assignment and show us size(c), and size(mean([min(c) max(c)]))


No, it is not z assignment.. first column have some data, and for same no. in first column it will find all data related with that no. and it will do mean of max and min found from all data and it will assigned to z..

Which line is it producing the error message against?

problem get solved..

I have changed only

    c = data(data(:,2)==i,1);

Thank you...!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!