problem with entering variables in a function

function [ output_args ] = Newfunction( h )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 105<h<120
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Approaching Stall'));
elseif 100<h<105;
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Close to stall'));
elseif h<100
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('STALL'));
end
end
**This is my function but i keep getting this error ??? Input argument "h" is undefined.
Error in ==> Newfunction at 5 if 105<h<120

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
編集済み: Azzi Abdelmalek 2013 年 3 月 8 日

0 投票

Use
if 105<h & h<120
and
elseif 100<h & h<105

10 件のコメント

Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
h=100
Newfunction=(h);
function [ output_args ] = Newfunction( h )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 105 < h && h < 120
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Approaching Stall'));
elseif 100<h && h <105;
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Close to stall'));
elseif h <100
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('STALL'));
end
*This is what I have done and i keep getting error messages ??? Input argument "h" is undefined.
Error in ==> Newfunction at 5 if 105 < h && h < 120
??? Error: File: Newfunction.m Line: 3 Column: 1 Function definitions are not permitted in this context. *
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
Remove this
Newfunction=(h);
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
I have removed it like you said. but it refers to my 'function' as error ??? Error: File: Newfunction.m Line: 2 Column: 1 Function definitions are not permitted in this context.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
Can you explain, from where are you calling this function? because your function begins by
function [ output_args ] = Newfunction( h )
Only when you want to call it from any object, you type
h=100
output_args = Newfunction( h )
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
Oh i get what you mean. I am not calling the function yet. because I am developing my GUI at the same time. So for now could i just set the
h=100
output_args = Newfunction(h)
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
h=108;
[ output_args ] = Newfunction( h );
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 105 < h && h < 120
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Approaching Stall'));
elseif 100<h && h <105;
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Close to stall'));
elseif h <100
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('STALL'));
end
that is my new function, it doesn't seem to have any errors but when I do run it it comes up with
??? Attempt to execute SCRIPT Newfunction as a function: /Users/Achchu/Documents/MATLAB/Newfunction.m
Error in ==> Newfunction at 2 [ output_args ] = Newfunction( h );
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
編集済み: Azzi Abdelmalek 2013 年 3 月 8 日
I am not sure that you know how function works. To test if your function is working save the above code as Newfunction
function output_args = Newfunction( h )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 105 < h && h < 120
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Approaching Stall'));
elseif 100<h && h <105;
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Close to stall'));
elseif h <100
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('STALL'));
end
Then from window command call your function:
h=100
output_args = Newfunction( h )
Note: You have not assigned any value to output_args, if it's intented then write
function Newfunction( h )
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
I have done what you said and when i type in h= 100 and output_args..... I am getting this error again
??? Input argument "h" is undefined.
Error in ==> Newfunction at 5 if 105 < h && h < 120
>> h=100
h =
100
>> output_args = Newfunction (h) Error in ==> Newfunction at 5 if 105 < h && h < 120
??? Output argument "output_args" (and maybe others) not assigned during call to "/Users/Achchu/Documents/MATLAB/Newfunction.m>Newfunction". >>
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
Thankyou for helping me btw.. this is a part of my final year project at university and I have a deadline in 2 weeks to get it all up and running.. thankyou so much
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
It is working now thankyou :)

サインインしてコメントする。

その他の回答 (1 件)

Ruben
Ruben 2013 年 3 月 8 日

0 投票

where and how do you call your function? for example if h = 100, you should call it like this:
Newfunction( 100 )
or
h=100;
Newfunction(h)

4 件のコメント

Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
i am writing this function in order for it to be called into a GUI. My 'h' should be something that changes as its a user input in the GUI. Is there any way i could make h a changing variable in the function. Or do I have to define 'h' for now and relate it back to the GUI when i callback the function
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 8 日
every time you call your function, wherever it is, you have to assign a value to h.
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013 年 3 月 8 日
do i have to assign this value in my GUI function. because I have assigned a value for h and wrote the function but still keep getting error messages
Ruben
Ruben 2013 年 3 月 8 日
as I see it now, you have assigned a value for h, but you're not supplying it to the function you wrote. There are two ways to overcome this.
1) supply the h as an input to your function every time you call it by calling your function like this: Newfunction(h) instead of: Newfunction
2) convert your function to a script. scripts don't require input, but are able to use all the current variables.
I'm no GUI-guru, so I don't know what is the best approach for your problem

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by