calling a function in a script

6 次查看(过去 30 天)
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

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-3-10
编辑:KALYAN ACHARJYA 2019-3-10
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-3-10
编辑:KALYAN ACHARJYA 2019-3-10
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 2019-3-12
Thank you Kalyan. I have managed to get it to work. I'm still getting used to the matlab structure.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by