How to return value from one function within Function Matlab

2 vues (au cours des 30 derniers jours)
Stephen john
Stephen john le 8 Avr 2022
Commenté : Voss le 8 Avr 2022
Hello Everyone, I hope you are doing well.
I have create a function Stages, which take the prediction from model, bases on prediction the case statement is used to get the value from other functions e.g DFUNCTION,SFunction ,etc
I want to return the values for each case for example the case "Class 1" is selected , the function DFUNCTION run it should return [DValue,Dlength,dLevels,Dmaximum,Dminimum]
if case "Class 3" is selected it returns [SlidMaximum,SlidMinimum,Slidmeans]
but i am unable to do that
How can i do it in matlab
function [labels]=Stages(ImagePath)
I = imread(ImagePath);
im=imresize(I,[227 227]);
im = repmat(im,[1 1 3]);
Net=load('trainedmodel.mat');
label = classify(Net.trainedNet,im);
labels=string(label);
switch labels
case "Class 1"
[DValue,Dlength,dLevels,Dmaximum,Dminimum] =DFUNCTION(ImagePath);
case "Class 2"
[SLevels,SValue,SMinimum,SMaximum] =SFunction(ImagePath);
case "Class 3"
[SlidMaximum,SlidMinimum,Slidmeans] = SlidFunction(ImagePath);
case "Class 4"
[PMaximum,PMinimum,Pmeans]= PFunction(ImagePath);
case "Class 5"
[jMaximum,jMinimum,Jmeans] = JFunction(ImagePath);
case "Class 6"
[fMaximum,fMinimum,fmeans] = FFunction(ImagePath);
end

Réponses (1)

Voss
Voss le 8 Avr 2022
Returning different sets of outputs from the function Stages, depending on the value of label
% One way
function varargout = Stages(ImagePath)
I = imread(ImagePath);
im=imresize(I,[227 227]);
im = repmat(im,[1 1 3]);
Net=load('trainedmodel.mat');
label = classify(Net.trainedNet,im);
switch label
case 'Class 1'
[varargout{1:nargout}] = DFUNCTION(ImagePath);
case 'Class 2'
[varargout{1:nargout}] = SFunction(ImagePath);
case 'Class 3'
[varargout{1:nargout}] = SlidFunction(ImagePath);
case 'Class 4'
[varargout{1:nargout}] = PFunction(ImagePath);
case 'Class 5'
[varargout{1:nargout}] = JFunction(ImagePath);
case 'Class 6'
[varargout{1:nargout}] = FFunction(ImagePath);
end
end
% Another way
function varargout = Stages(ImagePath)
I = imread(ImagePath);
im=imresize(I,[227 227]);
im = repmat(im,[1 1 3]);
Net=load('trainedmodel.mat');
label = classify(Net.trainedNet,im);
lookup = { ...
'Class 1' @DFUNCTION; ...
'Class 2' @SFunction; ...
'Class 3' @SlidFunction; ...
'Class 4' @PFunction; ...
'Class 5' @JFunction; ...
'Class 6' @FFunction};
[varargout{1:nargout}] = lookup{strcmp(lookup(:,1),label),2}(ImagePath);
end
  6 commentaires
Stephen john
Stephen john le 8 Avr 2022
Modifié(e) : Stephen john le 8 Avr 2022
@Walter Roberson Then How can i get output variables/arguments of cases if one is true.
for example
case "Class 3"
[SlidMaximum,SlidMinimum,Slidmeans] = SlidFunction(ImagePath);
the output should return SlidMaximum,SlidMinimum,Slidmeans
Voss
Voss le 8 Avr 2022
@Stephen john The error you ran into is because the label wasn't one of 'Class 1' through 'Class 6' (maybe it was a string - I'm using character vectors here).
To the point of the original question: The different functions DFUNCTION, etc., return different numbers of outputs. And it is not known at the time Stages is called which of the functions DFUNCTION, etc., will be called within Stages since that is determined by the classification label of the input ImagePath, which is calculated within Stages.
The code that calls Stages cannot know whether Stages will return 3 outputs (in case the label is 'Class 3') or 5 (in case it's 'Class 1'), for instance, so one thing you can do is to modify the functions DFUNCTION, etc., to all return the same number of outputs by including some empty outputs. Then call Stages with that number of outputs.
Here is a simplified example:
% DFUNCTION called -> 5 outputs
[out1,out2,out3,out4,out5] = Stages(1)
out1 = 0
out2 = 1
out3 = 2
out4 = 3
out5 = 4
% SFunction called -> 4 outputs with 5th "dummy" output returned here
[out1,out2,out3,out4,out5] = Stages(2)
out1 = 5
out2 = 6
out3 = 7
out4 = 8
out5 = []
% SlidFunction called -> 3 outputs with 4th and 5th "dummy" outputs returned here
[out1,out2,out3,out4,out5] = Stages(3)
out1 = 9
out2 = 10
out3 = 11
out4 = [] out5 = []
% SlidFunction called -> 3 outputs; only 3 outputs captured
[out1,out2,out3] = Stages(3)
out1 = 9
out2 = 10
out3 = 11
% DFUNCTION called -> 5 outputs, but only 3 outputs are captured here
% the other 2 are lost. That's why you'd have every function return 5 outputs
[out1,out2,out3] = Stages(1)
out1 = 0
out2 = 1
out3 = 2
function varargout = Stages(ImagePath)
label = sprintf('Class %d',ImagePath);
switch label
case 'Class 1'
[varargout{1:nargout}] = DFUNCTION(ImagePath);
case 'Class 2'
[varargout{1:nargout}] = SFunction(ImagePath);
case 'Class 3'
[varargout{1:nargout}] = SlidFunction(ImagePath);
end
end
function [DValue,Dlength,dLevels,Dmaximum,Dminimum] = DFUNCTION(ImagePath)
DValue = 0;
Dlength = 1;
dLevels = 2;
Dmaximum = 3;
Dminimum = 4;
end
function [SLevels,SValue,SMinimum,SMaximum,out5] = SFunction(ImagePath)
SLevels = 5;
SValue = 6;
SMinimum = 7;
SMaximum = 8;
out5 = [];
end
function [SlidMaximum,SlidMinimum,Slidmeans,out4,out5] = SlidFunction(ImagePath)
SlidMaximum = 9;
SlidMinimum = 10;
Slidmeans = 11;
out4 = [];
out5 = [];
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by