calling a function in a script

조회 수: 15 (최근 30일)
Sam Thorpe
Sam Thorpe 2019년 3월 10일
댓글: Sam Thorpe 2019년 3월 12일
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에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by