Why all the code in a MATLAB program are just functions

3 次查看(过去 30 天)
Hello,
I have a downloaded code which is all functions, and when I run it it (F5) it works.
The code is a GUI which execute image analysis functions.
When I contract all the function drop-downs (with Ctrl + "," ), all the code is based in funtions, there is no one no-function code, and it run correctly anyway when I execute it.
I don't understand that all the code runs without any no-function code, which in my understanding it would be the necessary for to run the functions and the code.
Thank you very much.
Josep
  2 个评论
Rik
Rik 2021-7-6
When you run the function with F5 it will run the first function in the file (technically there are exceptions, but let's not focus on those now).
Apparently that creates a figure with some interactive elements. When you interact with such elements, those will have callback functions. That means that you are actually calling one of those internal functions when you click on a button.
It therefore makes perfect sense that there are only functions: you ran the function that created the figure and the elements, and interacting with an element will call a specific function.
Did you do a basic Matlab tutorial? Do you have a specific question?
Josep Llobet
Josep Llobet 2021-7-6
I did the MATLAB Onramp, and some MATLAB Fundamentals, from MATLAB Academy.
I have tryied to comprobe the execution of the first funcion running the probafunction.m next code:
function probafunction
disp("hello")
end
function probafunction2
disp("Hello2")
end
Which displays:
>> probafunction
hello
Is just run the first function.
Modifying the first function inside:
function probafunction
disp("hello")
%Modifying that↓
probafunction2
end
function probafunction2
disp("Hello2")
end
The command Window displays the next:
>> probafunction
hello
Hello2
I came from Python, where if I am not wrong, the function does not execute unless it is defined in the script.
Thank you very much for the comment!

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-7-6
"No function code" is code written to a "script". Scripts share their workspace (the list of loclly used variables) with their caller. This has some sever disadvantages and professional code avoid scripts, but use functions with exactly defined inputs and outputs.
If a script does not use variables defined or used anywhere else, it can be converted to a function easily:
% This is my script file called "hello.m":
disp('hello')
To:
function hello
% This is my hello function stored in the file "hello.m":
disp('hello')
end
Both codes are equivalent and if you start them by F5 (green triangle) both do exactly the same.
"without any no-function code" is the standard case. Only tiny hacks or experimental code of beginners can be stored in scripts (an some very specific exceptions, where scripts are really the best choice).
  1 个评论
Josep Llobet
Josep Llobet 2021-7-6
Thank you very much Jan for the comments and advices about using functions and not scripts in the code, and for the code which you exemplifyies.
I didn't know that functions are executed without being defined in the script. I came from Python and R studio, where if I am not wrong functions needs to be defined in the scripts.
I didn't know about the fact that @Rik comment about is just the first function which run in the running of the script.
Thank you again

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by