Image processing: when to use script, when to use function
Ältere Kommentare anzeigen
Hello, I am fairly new to matlab. I've just completed my first code. My instructors have told me to convert my script.m file into a function file. I hope this isn't a noob question but...Firstly, I'm confused why it matters. I've read the difference between scripts and functions, but I'm having trouble understanding the difference practically. Secondly, I'm not sure how to convert this into a function. For this code, I provide an image, I select a few points, and the image is rotated. I have several other scripts (to free hand crop, to paste images together, and to plot fluorescence). I'm not sure if these should be converted to functions as well...any help here would be very helpful thanks!
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 18 Dez. 2013
0 Stimmen
Convert scripts to a function when you want to call them over and over again with different parameters, such as filename, window width, or whatever. You can even make a chunk of your code into a function if you plan on calling it only once but you just want to simplify your code into a few main concepts. So you can call a just a few functions in your main routine, and the functions are essentially black boxes where a lot of the heavy lifting occurs, but whose code does not clutter up your main routine.
Walter Roberson
am 18 Dez. 2013
0 Stimmen
In addition to what Image Analyst wrote:
When you convert to a function you should likely get rid of the "clc", "close all", "imtool close all", and "workspace" commands, as whatever routine that is calling your function might have need of something you are turning off with those.
In your code you assign two values to variables using input(). Likely those two values should become parameters to the function, so that the calling routine can determine appropriate values in any way it sees fit (such as through a real GUI.)
One reason to use a function that Image Analyst did not happen to mention, is that MATLAB is able to execute functions more efficiently than it can execute scripts. The difference in execution time between the two was reduced a fair bit just a couple of releases ago, but functions are still expected to be more efficient.
Also, when you are in the editor, MATLAB can do better error analysis for functions than for scripts.
Kategorien
Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!