calling a function in a script

14 Ansichten (letzte 30 Tage)
Sam Thorpe
Sam Thorpe am 10 Mär. 2019
Kommentiert: Sam Thorpe am 12 Mär. 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

Akzeptierte Antwort

KALYAN ACHARJYA
KALYAN ACHARJYA am 10 Mär. 2019
Bearbeitet: KALYAN ACHARJYA am 10 Mär. 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 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 10 Mär. 2019
Bearbeitet: KALYAN ACHARJYA am 10 Mär. 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 am 12 Mär. 2019
Thank you Kalyan. I have managed to get it to work. I'm still getting used to the matlab structure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by