1 / 20

Programmer Defined Functions Common Errors

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.

palila
Download Presentation

Programmer Defined Functions Common Errors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 [ ]

  2. Error1: using F5 • Assume this function definition:

  3. 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!

  4. 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.

  5. 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.

  6. 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.)

  7. 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.

  8. 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.

  9. 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?”

  10. Correct Function File:

  11. 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.

  12. Error 4: Using clear Problem: it says my variable was cleared, though I indicated them as parameters.

  13. 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???

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. 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

  19. 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.

  20. 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.

More Related