200 likes | 356 Views
Programmer Defined Functions Common Errors. Using F5 "Nothing seems to be different between each run" Lots of words underlined orange Using clear Prompt again within the function Forgetting the commas, and the [ ]. Error1: using F5. Assume this function definition:. Error1: using F5.
E N D
Programmer Defined FunctionsCommon Errors Using F5 "Nothing seems to be different between each run" Lots of words underlined orange Using clear Prompt again within the function Forgetting the commas, and the [ ]
Error1: using F5 • Assume this function definition:
Error1: using F5 We all do it since it is a reflex! The value of variable a is unknown to you, to me, to Matlab….. So Matlab screams.. Just like you would, or I would!
Error1: F5 does nothing. Solution: • Manually change directory to the one that has the function file. • Write the function-call. This is the only way to get the code in a function file to run.
Error2: Nothing seems to change… Problem: I just made changes to my function file. I run the function-call again but nothing seems different. Answer: I probably forgot to save the latest version of the function file. F5 was a built-in: save&run. Since F5 is useless now, save (Ctrl+S) manually before running… Hint: if there is a little star by the function’s name, it has not been saved.
Error3: Underlined “stuff” Problem: Lots of underline orange in the function file. THESE ARE NO LONGER WARNINGS. MUST BE FIXED. (Why isn’t it red then? Matlab doesn’t know if this will really get used. Orange is warning. When it could be ignored in a regular script file, it MUST NOT be ignored here.)
The parameter side… • RULE1: ALL ARGUMENTS (INPUTS) MUST BE USED, NO EXCEPTIONS. Input argument ‘Wsatellite’ might be unused. Error message when mouse stays on the word for a second. A simple typo makes those two variables un-related.
The return-value side… • RULE2: ALL RETURN-VALUES (OUTPUTS) MUST BE ASSIGNED A VALUE, NO EXCEPTIONS. The function return value ‘W_payload’ might be unset. Error message when mouse stays on the word for a second. Matlab is looking for: W_payload = something; but not finding it… Rrrrr. A typo again.
The code-body side… • NOT A RULE, BUT COMMON SENSE. Why store a result in a variable if it is not used later? The value assigned to ‘Wtelemetry‘ might be unused. Error message when mouse stays on the word for a second. If you haven’t figured this out yet, typos are mean. Matlab is asking: “Why are you wasting my memory?”
Again… RULE1: ALL ARGUMENTS (INPUTS) MUST BE USED, NO EXCEPTIONS. V1 = a+b+c;… something like that. RULE2: ALL RETURN-VALUES (OUTPUTS) MUST BE ASSIGNED A VALUE, NO EXCEPTIONS.
Error 4: Using clear Problem: it says my variable was cleared, though I indicated them as parameters.
Error 4: Using clear Solution: NEVER use clear. Seriously, some solutions are easy! a comes in with a value (from the function-call) But then you decide to delete the workspace.. (which deletes all variables, so that includes a)… So what exactly is Matlab (or you) supposed to use here???
Error 5: Prompting for the parameters again... The arguments are given to a function, so to prompt for them again would erase the original values. BAD. Imagine this: result = sind(30); and having the angle re-asked when the function runs. The parameter list are the INPUTS to a function. I-N-Puts, i.e. GIVENS, i.e. KNOWN, i.e. already-has-values.
Error 5: Prompting for the parameters again... Honestly: This problem only occurs with students who don't know how to "call-a-function" Solution: Go learn how to "call a function"!!! The ONLY time a function may prompt for one of the parameters is if it error-proof each before calculating data.
Note ALL THESE PROBLEMS CAN BE FIXED WITH COMMON SENSE BUT, IT ONLY MAKES SENSE IF YOU UNDERSTAND (LEARN) THE LOGIC OF HOW A FUNCTION WORKS.
Error 6: Syntax errors • Unfortunately, even common sense does not help with some problems: those that require to know the vocabulary and symbols used by Matlab. • For example (amongst many errors possible): • Multiple parameters must be separated by commas. • Multiple return-values must be enclosed in square brackets. • Omit the equal sign if there is no return value.
Error 6: Forgetting Commas • Commas are mandatory in the parameter list. Otherwise, the second variable gets red-underlined! • However, commas are optional in the return-info list!! To avoid loosing your mind: • Peacefully understand what and why? • or Just put commas everywhere and be consistent… aconaz.org
Error7: Forgetting the [ ] • When there are multiple return values, [ ] are mandatory. MATLAB otherwise gets confused with the name of the function. • Why? MATLAB sees no return values, so the next 'thing' MATLAB expects in the function file is: <function name>(<parameter list>). Since the name of a function SHOULD be the name of the file, MATLAB indicates that problem now.
Wrapping Up • Do not use F5. Thought it may "change directory" once you click ok, it cannot possibly run the actual file when there is a list of parameters. • Rule1: use all the parameters in the code-body • Rule2: assign all the return values (regardless of all the if/switch/loops, etc…) • Rule3: don't store values in variables if they are not to be used later • Don't write clear • Don't ask for the same inputs as the parameters • Correctly place commas and [ ] in the right spots.