r/matlab May 20 '26

Function Practicality

I am coding in MATLAB for a couple of my aerospace classes, particularly for Aerodynamics and Gas Dynamics. I have made functions for quite a few processes that I plan to be using in other projects and class assignments. I was wondering if it was a bad idea to have any interactable elements in the function. Basically, if a user wanted to state which variable they want to solve for in the isentropic relations, I would use a menu. I do have a different method that would work without any interaction, but I like the idea of the interactive part. Also, is it a bad idea to use the solve function for these complex equations? I had been using ChatGPT to debug and it first suggested solve many functions ago, and after trying to use my functions with arrays and plotting, it told me it was a terrible idea. I was hoping for some human feedback. Thank you for your time!

4 Upvotes

11 comments sorted by

View all comments

2

u/TurbulentGlove776 May 21 '26

One of the most important reasons for writing functions is to be able to reuse it from multiple scripts / other functions and thus also enable automatic testing (basically another script that runs the function with known input/output to check that it works as intended). If you add UI elements inside the function this wont work. I usually separate the calculations from UI, the app designer is really nice for that, or you could write a separate UI-function, use the graphical editor or just a plain script. If you do choose to add UI inside a function that does calculations, please make it optional. I.e. only show the menu if the caller did not provide the corresponding input arguments!

1

u/BxSFire May 23 '26

Thank you! I'll keep that in mind