How to make M-File with function and "header"
Show older comments
Hello, I am facing a little problem as i describe:
I need to make .m file with function that receives data compute it and give back the result, this is easy (I know how to make this). But in this M-file must be included names of parameters of this function and their default values that I can access from another M-file.
for example:
function [m,s] = func1(arg1,arg2,arg3)
%calculation here
[m,s];
end
and the "header" in same M-file should give:
- arg1,arg2,arg3 as strings
- and default values of arg1,...,arg3 as number
when other M-file ask for them
Is this possible and how to make it?
1 Comment
I cannot imagine in which situation this could be useful. What does "header" exactly mean and how does it "give"?
Answers (2)
Perhaps you want:
function [m,s] = func1(arg1,arg2,arg3)
if nargin == 0
% Reply names of inputs and default values:
m = {'arg1','arg2','arg3'};
s = {1,2,3};
return;
end
%calculation here
[m,s];
end
Are you really sure that this is useful? The internally used names of the variables in the input list should not matter in the caller.
Youssef Khmou
on 7 Mar 2013
Edited: Youssef Khmou
on 7 Mar 2013
hi Micheal,
For example in C/C++, a header file contains predefined function such that including a header file ( example "math.h") in Main file imports the predefined functions, but here using Matlab, you can use nested functions and/ or external functions and /or Mex files written in C/C++ :
example :
%-------------------------------------------------
function Y=function_Name(a,b)
n=size(a);
% .......
% ........
Y=Mean_Modulus(a,b);
% Nested function
function Z=Mean_Modulus(a,b);
Z=mod(a,b);
Z=mean(Z(:));
return
%-------------------------------------------
So nested functions can replace header Files , that is my personal approach
I hope this helps
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!