How to create pop out warning messages?

I am trying to create a function that will display messages by comparing a certain input. How can I display messages at each stage, which is like a warning box. Please help
function [ output_args ] = Newfunction( velocity )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if (cond)
velocity >= 120;
h=msgbox('Your speed is okay');
else if velocity >=100 & velocity <=100;
h=msgboc('Warning:Approaching Stall');
else
h=msgbox('STALL');
end
end

Antworten (3)

Image Analyst
Image Analyst am 6 Mär. 2013

0 Stimmen

warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', velocity);
uiwait(warndlg(warningMessage));
The uiwait is in there so your code does not go blasting merrily along. The uiwait() will cause the code to pause until the user clicks OK.
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan am 6 Mär. 2013

0 Stimmen

function [ output_args ] = Newfunction( h )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 120<h<105
then warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);uiwait(warndlg('Approaching Stall'));
else if 105<h<100;
then warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h); uiwait(warndlg('Close to stall'));
else if h<100
then warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);uiwait(warndlg('STALL'));
end
end
end
It comes up as h is undefined as an error. h is a variable that changes and it is a user input, how can i insert a variable into it so i can get rid of the error. thankyou so much for the help so far guys

9 Kommentare

Walter Roberson
Walter Roberson am 6 Mär. 2013
Caution: 120<h<105 is interpeted as ((120<h)<105) which is ((0 or 1) < 105) which is always true. To test a range use (105 < h & h < 120)
Walter Roberson
Walter Roberson am 6 Mär. 2013
We need to see how you are invoking Newfunction
function [ output_args ] = Newfunction( h )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 105<h<120
then warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h); uiwait(warndlg('Approaching Stall')); else if 100<h<105;
then warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Close to stall'));
else if h<100
then warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('STALL'));
end
end
end
*that's what i've done so far and i keep getting the error message ??? Input argument "h" is undefined.
Error in ==> Newfunction at 5 if 105<h<120*
When i try to use & it comes up asking me to use && which then invalidates my parameters which has to be between 105-120 and so on rather than be both 105 and 120 :/
Image Analyst
Image Analyst am 6 Mär. 2013
You don't actually have the "then" word in there, do you? That's for Visual Basic, not MATLAB. Also, elseif is one word, not two.
Do you know how to use the debugger? If you did, you'd simply step into this code and see what the value of h is. Apparently it's nothing because you didn't pass anything it.
How are you starting the code running? Are you going to Newfunction in the editor and pressing F5 or selecting run from the menus? If you are, then you cannot do that when you want to pass in parameters. You need to instead go to the command line and give a command such as
Newfunction(108)
I recommend that you fix your "if" conditions as I described above. (Ask the interface here to expand comments)
I have fixed my if conditions. would i have to insert Newfunction (108) at the start of the 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
You can put that function anywhere in the m-file. But don't do this:
if 105<h<120
do
if 105 < h && h < 120
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
I have done what you advised me to do, I am still getting the error message. I apologise I am fairly new to all this. where do you think i seem to be going wrong
??? Input argument "h" is undefined.
Error in ==> Newfunction at 5 if 105 < h && h < 120
How are you starting the code running? Are you going to Newfunction in the editor and pressing F5 or selecting run from the menus? If you are, then you cannot do that when you want to pass in parameters. You need to instead go to the command line and give a command such as
Newfunction(108)
Notice that is at the command line, not part of your code. It would instruct MATLAB to actually run the code, passing in the value 108 for h. Change the 108 to the value you want to give to h for your run.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by