use of function in if statement
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Nick
il 14 Nov 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
Risposte (2)
Cris LaPierre
il 14 Nov 2018
Modificato: Cris LaPierre
il 14 Nov 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.
0 Commenti
dpb
il 14 Nov 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!