use of function in if statement

13 次查看(过去 30 天)
ET1994
ET1994 2018-11-14
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 个评论
Nick
Nick 2018-11-14
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

请先登录,再进行评论。

回答(2 个)

Cris LaPierre
Cris LaPierre 2018-11-14
编辑:Cris LaPierre 2018-11-14
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 2018-11-14
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.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by