calling a function in a script

13 ビュー (過去 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 Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by