calling a function in a script

13 views (last 30 days)
Sam Thorpe
Sam Thorpe on 10 Mar 2019
Commented: Sam Thorpe on 12 Mar 2019
Hi. I have defined the following function which I wish to call in a script:
function imageprocess=imselect(x);%function to select same image from each group
imagescontg=zeros(1,10);
imagescontr=zeros(1,10);
imagestreatg=zeros(1,10);
imagestreatr=zeros(1,10);
for n=x:size(imagescontg,2)
imagecontg{1,n}=imread(sprintf('group1 g%2d.jpg',n)); %selects green image (n) from list
imagecontr{2,n}=imread(sprintf('group1 r%2d.jpg',n)); %selects red image (n) from list
a=rgb2gray(imagecontg{1,n}); %convert green image to greyscale
b=rgb2gray(imagecontr{2,n}); %convert red image to greyscale
imshowpair(a,b,'montage');%compare images to one another and plot
totintcontg(n)=sum(sum(a)); %equation to find the total intensity value of the green image
totintcontr(n)=sum(sum(b)); %equation to find the total intensity value of the red image
imagetreat{1,n}=imread(sprintf('group4 g%2d.jpg',n)); %selects green image (n) from list
imagetreat{2,n}=imread(sprintf('group4 r%2d.jpg',n)); %selects red image (n) from list
c=rgb2gray(imagetreat{1,n}); %convert green image to greyscale
d=rgb2gray(imagetreat{2,n}); %convert red image to greyscale
imshowpair(c,d,'montage');%compare images to one another and plot
totinttreatg(n)=sum(sum(c)); %equation to find the total intensity value of the green image
totinttreatr(n)=sum(sum(d)); %equation to find the total intensity value of the red image
end
end
what I am having trouble with is matlab allowing me to call the function. So I would define the first value as x=1 and then get the results from the function so I could process them further in the script. e.g.
startvalue = x
x=1
function imageprocess=imselect(x);%function to select same image from each group
imagescontg=zeros(1,10);
imagescontr=zeros(1,10);
imagestreatg=zeros(1,10);
imagestreatr=zeros(1,10);
for n=x:size(imagescontg,2)
imagecontg{1,n}=imread(sprintf('group1 g%2d.jpg',n)); %selects green image (n) from list
imagecontr{2,n}=imread(sprintf('group1 r%2d.jpg',n)); %selects red image (n) from list
a=rgb2gray(imagecontg{1,n}); %convert green image to greyscale
b=rgb2gray(imagecontr{2,n}); %convert red image to greyscale
imshowpair(a,b,'montage');%compare images to one another and plot
totintcontg(n)=sum(sum(a)); %equation to find the total intensity value of the green image
totintcontr(n)=sum(sum(b)); %equation to find the total intensity value of the red image
imagetreat{1,n}=imread(sprintf('group4 g%2d.jpg',n)); %selects green image (n) from list
imagetreat{2,n}=imread(sprintf('group4 r%2d.jpg',n)); %selects red image (n) from list
c=rgb2gray(imagetreat{1,n}); %convert green image to greyscale
d=rgb2gray(imagetreat{2,n}); %convert red image to greyscale
imshowpair(c,d,'montage');%compare images to one another and plot
totinttreatg(n)=sum(sum(c)); %equation to find the total intensity value of the green image
totinttreatr(n)=sum(sum(d)); %equation to find the total intensity value of the red image
end
end
% further processing in the script
Controlledgroupresults = [totintcontg; totintcontr]'
Treatmentgroupratioresults = [totinttreatg; totinttreatr]'
Controlledratio=[totintcontg./totintcontr]'
Treatmentration=[totinttreatg./totinttreatr]'
[h,p]=ttest(Controlledratio,Treatmentration)
Does anyone have any ideas?
thanks

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 10 Mar 2019
Edited: KALYAN ACHARJYA on 10 Mar 2019
Is there any issue to call the function.
Look, suppose you have the following function
function imageprocess=imselect(x);
%do something
end
%save it in different matlab file name as imselect
Now call the function in main script
x=.. % define the value
y=imselect(x);
Here imageprocess output as y.
In your second code, why you are defining the function code again, not required.
  3 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 10 Mar 2019
Edited: KALYAN ACHARJYA on 10 Mar 2019
Have you tried this one, after define x= some value (say 1) in main script, not in function code?
y=imselect(x)
Sam Thorpe
Sam Thorpe on 12 Mar 2019
Thank you Kalyan. I have managed to get it to work. I'm still getting used to the matlab structure.

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!