필터 지우기
필터 지우기

use of function in if statement

조회 수: 13 (최근 30일)
ET1994
ET1994 2018년 11월 14일
편집: Cris LaPierre 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