Calling Functions Files within a Script File

3 次查看(过去 30 天)
I'm writing a GUI script and I'm trying to streamline the code. To do this, I'm creating functions that perform specific processes and calling them from within the main script. The problem is that I haven't been able to call them effectively.
I thought all I would need to do is the following:
% --- Executes on selection change in popupmenu_filters.
function popupmenu_filters_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_filters (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
filters
where "filters" is the function that I'm calling and I have a filters.m file saved in the same folder. Is there something wrong with my syntax?
  1 个评论
Jan
Jan 2012-7-31
Do you get an error message? If so, please post a complete copy and the line, which causes the error.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2012-7-30
There are three kinds of .m files: scripts, functions, and class definitions. Class definition .m files have the word "classdef" as the first non-comment word in the .m file. Function .m files have the word "function" as the first non-comment word in the .m file. Script files never have "function" appearing other than in a comment or a string. If you have a script that has the executable line
function popupmenu_filters_Callback(hObject, eventdata, handles)
then MATLAB will refuse to process the file.
If your "GUI script" is actually a GUI function file, then it is okay to have that line in the file.
Does the function "filters" require any parameters? If so then you must pass the parameters on the command line, such as
filters('z17q9', 'bandpass', [3 1 4 1 5 9])
  3 个评论
Walter Roberson
Walter Roberson 2012-7-31
MATLAB program files can be either scripts or functions. Scripts are simply files containing a sequence of MATLAB statements. Functions make use of their own local variables and accept input arguments.
There are two kinds of program files:
Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace.
Functions, which can accept input arguments and return output arguments. Internal variables are local to the function.
Therefore, a .m file can be either a "script" or a "function", but a "script" can never ever contain a "function" (except as a comment or part of a string.)
Look at the very first (non-comment) word in your .m file: if that word is "function", then the .m file is a function file, and is not a script.
Show us the first non-blank non-comment line of your .m file, and also the first non-blank non-comment line of your filters.m file.
Caleb
Caleb 2012-7-31
I see what you're saying. I got it to work after I stopped trying to treat it like a script file. Thank you.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write Unit Tests 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by