How to return value from one function within Function Matlab

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

Respuestas (1)

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 comentarios

@_ both are not working
the second method gives error
Intermediate brace '{}' indexing produced a comma-separated list with 0 values, but it must produce a single value when followed by subsequent indexing operations.
Error in Stages (line 15)
[varargout{1:nargout}] = lookup{strcmp(lookup(:,1),label),2}(ImagePath);
Related documentation
@_ The first method tell only number of arguments not values of arguments
What do you want your code to do if the function is run without any output arguments being requested?
.... I have a suspicion that you are hoping to run the function with no output assignments, and want it to assign to variables with those particular names anyhow. There are situations in which MATLAB would allow that to be done, but in most circumstances it should be avoided.
@Walter Roberson i want the output arguments as a variable . for example
if the labels has ouput
"Class 1" is selected , the function DFUNCTION run it should return [DValue,Dlength,dLevels,Dmaximum,Dminimum]
This values should be displayes on the main file
Stephen john
Stephen john el 8 de Abr. de 2022
Editada: Stephen john el 8 de Abr. de 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
@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

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2021b

Preguntada:

el 8 de Abr. de 2022

Comentada:

el 8 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by