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

how to make code global

Asked by Lalit Patil on 21 Nov 2012

clear all;

clc;

RGB = imread('Image100.jpg');

GRAY = rgb2gray(RGB);

threshold = graythresh(GRAY);

originalImage = im2bw(GRAY, threshold);

i = bwareaopen(originalImage,350);

imshow(i)

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

end

fclose(fid);

data = textread('lalit1.txt');

r = unique(data);

for i=r',

    c = data(data(:,1)==i,2);
    z(i,1) = mean([min(c) max(c)]);

end

fid = fopen('lalit2.txt','wt');

for j=1:length(r)

fprintf(fid,'%f %f\n',r(j),z(j));

end

fclose(fid);

B = 1:480;

B = B';

missingvalues = setdiff(B,r);

fid = fopen('lalit3.txt','wt');

for j=1:length(missingvalues)

q(j) = 0;

fprintf(fid,'%f %f\n',missingvalues(j),q(j));

end

fclose(fid);

outfid = fopen('all_Image.txt', 'wt');

for j = 2 : 3

filename = ['lalit' num2str(j),'.txt'];
fwrite(outfid, fileread(filename));

end

fclose(outfid);

This is the code i have written for one image.. I want to use this code for 100 images named Image1,Image2,Image3... So, i used global for this and i completed half as below.. and it works..

clear all;

clc;

for k = 1:200

global filename

filename = ['Image' num2str(k),'.jpg' ];

RGB = imread(filename);

GRAY = rgb2gray(RGB);

threshold = graythresh(GRAY);

originalImage = im2bw(GRAY, threshold);

i = bwareaopen(originalImage,350);

m = max(max(i));

[r c] = find(i == m);

global filename

filename = ['Image' num2str(k),'.txt' ];

fid = fopen(filename,'wt');

for j=1:length(r)

fprintf(fid,'%f %f\n',r(j),c(j));

end fclose(fid);

global filename

filename = ['Image' num2str(k),'.txt' ];

data = textread(filename);

r = unique(data);

for i=r',

    c = data(data(:,1)==i,2);
    z(i,1) = mean([min(c) max(c)]);

end

global filename

filename = ['data' num2str(k),'.txt' ];

fid = fopen(filename,'wt');

for j=1:length(r)

fprintf(fid,'%f %f\n',r(j),z(j));

end

But i can't

understand next what to do.. I tried but can't do.. So,please help me...!

1 Comment

Jan Simon on 21 Nov 2012

Please format your code properly. Currently it is not readable.

Lalit Patil

Tags

Products

2 Answers

Answer by Pedro Villena on 21 Nov 2012
Edited by Pedro Villena on 22 Nov 2012
Accepted answer
 >>arrayfun(@(i) myFunction(i),1:100);

myFunction.m

function [] = myFunction(imageNumber)
%RGB = imread('Image100.jpg'); 
RGB = imread(sprintf('Image%d.jpg',imageNumber));
GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);
i = bwareaopen(originalImage,350);
%imshow(i),
m = max(max(i));
[r c] = find(i == m);
%fid = fopen('lalit1.txt','wt');
fid = fopen(sprintf('Image%d_lalit1.txt',imageNumber),'wt');
for j=1:length(r),
    fprintf(fid,'%f %f\n',r(j),c(j));
end
fclose(fid);
%data = textread('lalit1.txt');
data = textread(sprintf('Image%d_lalit1.txt',imageNumber));
r = unique(data);
%z = zeros(size(r));
for i=r',
    c = data(data(:,1)==i,2);
    z(i,1) = mean([min(c) max(c)]);
end
%fid = fopen('lalit2.txt','wt');
fid = fopen(sprintf('Image%d_lalit2.txt',imageNumber),'wt');
for j=1:length(r),
    fprintf(fid,'%f %f\n',r(j),z(j));
end
fclose(fid);
B = 1:480;
B = B';
missingvalues = setdiff(B,r);
%fid = fopen('lalit3.txt','wt'); 
fid = fopen(sprintf('Image%d_lalit3.txt',imageNumber),'wt'); 
%q = zeros(1,length(missingvalues));
for j=1:length(missingvalues),
    q(j) = 0; 
    fprintf(fid,'%f %f\n',missingvalues(j),q(j)); 
end
fclose(fid);
%outfid = fopen('all_Image.txt', 'wt'); 
outfid = fopen(sprintf('Image%d_all.txt',imageNumber), 'wt'); 
for j = 2:3,
    %filename = ['lalit' num2str(j),'.txt'];
    filename = sprintf('Image%d_lalit%d.txt',imageNumber,j);
    fwrite(outfid, fileread(filename));
end
fclose(outfid);

1 Comment

Lalit Patil on 21 Nov 2012

Thank you very much.. I never thinked about to make a function.. This helps me a lot, and gives me an idea to use code as a function..!

Pedro Villena
Answer by Jan Simon on 21 Nov 2012

I do not see a reason to use global variables here. But it is hard to see anything in the code without a proper formatting.

Which detail of your code does not work? What happens when you run the code?

It is a good idea to omit all parts of the code, which are working correctly, when you post it in the forum. Then we can see much faster, where the modifications are needed.

1 Comment

Lalit Patil on 21 Nov 2012

Actually the above code without global is only for one image.. If i want to use the same code for 10 images then how to use loops in that code.. I think global can do this and so that i write another code.. But i can write only half code and i want help to write remaining code and stucked at half of this code..

I am attaching 10 images here... http://speedy.sh/7Zc2P/Image.zip

Jan Simon

Contact us