Why all the code in a MATLAB program are just functions

조회 수: 3 (최근 30일)
Josep Llobet
Josep Llobet 2021년 7월 6일
댓글: Josep Llobet 2021년 7월 6일
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