Borrar filtros
Borrar filtros

use of function in if statement

13 visualizaciones (últimos 30 días)
ET1994
ET1994 el 14 de Nov. de 2018
Editada: Cris LaPierre el 14 de Nov. de 2018
Hi everybody,
Trying to create a program for basic calculation involving if statement and function.
Below having error;
Can someone suggest an idea please.
input = str2num(get(hObject,'StrTing'));
%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
set(hObject,'String','0')
otherwise
function childbirth = nb(x1);
cover = input('Enter the cover plan: ');
if cover == 1
% if condition is true then the reimbursement will be 40 percent of the
% amount entered for the bill in x1
childbirth = x1 .* (40/100);
elseif (cover == 2)
childbirth = x1 .* (60/100);
elseif cover == 3
childbirth = x1 .* (80/100);
else
disp('The number entered is wrong, please verify!')
end
end
end
guidata(hObject, handles);
  1 comentario
Nick
Nick el 14 de Nov. de 2018
It would be nice to mention the exact error message.
From the first look its not in a switch statement so otherwise has to be replaced by else. Also calling a function does not require the function part, thats only needed for the function definition which you could do at the end of your function file or in a seperate function file

Iniciar sesión para comentar.

Respuestas (2)

Cris LaPierre
Cris LaPierre el 14 de Nov. de 2018
Editada: Cris LaPierre el 14 de Nov. de 2018
It not clear to me what you are trying to do. Does the funciton nb already exist or are you trying to define it in the if statement?
MATLAB supports functions defined in a script, but they must be placed at the very bottom of the script. You can then call it in the if statement by its name
input = str2num(get(hObject,'StrTing'));
...
if (isempty(input))
...
else
childbirth = nb(x1);
...
%% in-file functions
function out = nb(x1);
...
end
There are other issues that would need to be fixed in your code as well.
  • otherwise can only be used with a switch statement. Use "else" in an if statement
  • Is this code coming from a gui in Guide? If so, then discount my example. You can still declare a function in a GUI, but it is done differently.

dpb
dpb el 14 de Nov. de 2018
First and foremost, to place a function in a script or other function as local, it must be outside a logic construct...you then refer to it in the normal ML syntax manner:
input = str2num(get(hObject,'StrTing'));
%checks to see if input is empty. if so, default input1_editText to zero
if (isempty(input))
set(hObject,'String','0')
else
set(hObject,'String',str2num(childbirth(nb(x1))))
end
guidata(hObject, handles);
...
function childbirth = nb(x1);
cover = input('Enter the cover plan: ');
if cover == 1
% if condition is true then the reimbursement will be 40 percent of the
% amount entered for the bill in x1
childbirth = x1 .* (40/100);
elseif (cover == 2)
childbirth = x1 .* (60/100);
elseif cover == 3
childbirth = x1 .* (80/100);
else
disp('The number entered is wrong, please verify!')
end
end
may be more like what you're trying to do as a guess.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by