Function that returns information about other functions

7 Ansichten (letzte 30 Tage)
Mohamad Jamil Al Bouhairi
Mohamad Jamil Al Bouhairi am 25 Mai 2021
Kommentiert: John D'Errico am 25 Mai 2021
I want to write a function getFunction() that takes as input a function name and returns it's argument names and their types. For example, a function s = add(x,y) would output the sum of x + y. x, y and s are all numeric integers.
If I pass it in getFunction(), like getFunction('add'), it would return that 'add' needs two inputs:
  1. argument name: x and argument type: int
  2. argument name: y and argument type: int
Any help would be much appreciated.
  2 Kommentare
Geoff Hayes
Geoff Hayes am 25 Mai 2021
Mohammad - do you mean to write a function similar to help? Are you expecting that you would need to add comments to your functions so that when calling either getFunction or help, then these comments would be returned?
Mohamad Jamil Al Bouhairi
Mohamad Jamil Al Bouhairi am 25 Mai 2021
@Geoff Hayes Not quite. Adding help to a program would reuire me to insert comments myself. I was looking for another implementation where getFunction() automatically checks the function arguments of the passed in function and returns the argument/input names and their types.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 25 Mai 2021
You're not going to be able to do what you described, at least not without a ton of work and a lot of restricting what your function actually is supposed to do.
What were you hoping to do with this information? If we know that maybe we can help you find another way to achieve it.
Why do you care what names the add function uses internally to store the data with which you call it? Just because the function internally uses the names x and y that doesn't have any bearing on what the data that you pass into the function is called, or even if it has a name. All these calls in the comment section below (assuming x, y, hocus, and pocus are defined) are perfectly valid calls to your add function.
%{
add(x, y)
add(hocus, pocus)
add(magic(4), rand(4))
%}
Why should the name 'add' find your function first? What if you had called your function 'plus' instead? Why should it take precedence over any of the (numerous) other plus functions included in MathWorks products?
which -all plus
built-in (/MATLAB/toolbox/matlab/ops/@char/plus) % char method built-in (/MATLAB/toolbox/matlab/ops/@double/plus) % Shadowed double method built-in (/MATLAB/toolbox/matlab/ops/@int16/plus) % Shadowed int16 method built-in (/MATLAB/toolbox/matlab/ops/@int32/plus) % Shadowed int32 method built-in (/MATLAB/toolbox/matlab/ops/@int64/plus) % Shadowed int64 method built-in (/MATLAB/toolbox/matlab/ops/@int8/plus) % Shadowed int8 method built-in (/MATLAB/toolbox/matlab/ops/@logical/plus) % Shadowed logical method built-in (/MATLAB/toolbox/matlab/ops/@single/plus) % Shadowed single method built-in (/MATLAB/toolbox/matlab/ops/@uint16/plus) % Shadowed uint16 method built-in (/MATLAB/toolbox/matlab/ops/@uint32/plus) % Shadowed uint32 method built-in (/MATLAB/toolbox/matlab/ops/@uint64/plus) % Shadowed uint64 method built-in (/MATLAB/toolbox/matlab/ops/@uint8/plus) % Shadowed uint8 method plus is a built-in method % Shadowed string method /MATLAB/toolbox/matlab/datatypes/duration/@duration/plus.m % Shadowed duration method /MATLAB/toolbox/matlab/datatypes/datetime/@datetime/plus.m % Shadowed datetime method /MATLAB/toolbox/matlab/timeseries/@timeseries/plus.m % Shadowed timeseries method plus is a built-in method % Shadowed pattern method plus is a built-in method % Shadowed connector.internal.LoggerLevel method plus is a built-in method % Shadowed matlab.internal.lang.capability.Capability method plus is a built-in method % Shadowed matlab.lang.OnOffSwitchState method /MATLAB/toolbox/matlab/timeseries/+tsdata/@datametadata/plus.m % Shadowed tsdata.datametadata method plus is a built-in method % Shadowed matlab.internal.reference.property.RefEntityType method plus is a built-in method % Shadowed matlab.internal.reference.api.EntityPrecision method plus is a built-in method % Shadowed matlab.internal.reference.property.DeprecationStatus method plus is a built-in method % Shadowed matlab.internal.reference.property.FunctionType method plus is a built-in method % Shadowed matlab.internal.reference.api.EntityCaseSensitivity method plus is a built-in method % Shadowed dig.config.CommandType method plus is a built-in method % Shadowed dig.model.DisplayState method plus is a built-in method % Shadowed dig.model.EventDataType method plus is a built-in method % Shadowed dig.model.FunctionType method plus is a built-in method % Shadowed dig.model.ValidInBdType method plus is a built-in method % Shadowed simulink.FindSystemTask.Status method plus is a built-in method % Shadowed targetrepository.DataSource method plus is a built-in method % Shadowed RecordBlkView.AxisColor method plus is a built-in method % Shadowed RecordBlkView.GridDisplay method plus is a built-in method % Shadowed RecordBlkView.LayoutType method plus is a built-in method % Shadowed RecordBlkView.LegendPosition method plus is a built-in method % Shadowed RecordBlkView.LineStyle method plus is a built-in method % Shadowed RecordBlkView.MapLayer method plus is a built-in method % Shadowed RecordBlkView.ScalingMode method plus is a built-in method % Shadowed RecordBlkView.SnapshotArea method plus is a built-in method % Shadowed RecordBlkView.SnapshotSend method plus is a built-in method % Shadowed RecordBlkView.TickLabelsDisplay method plus is a built-in method % Shadowed RecordBlkView.TickPosition method plus is a built-in method % Shadowed RecordBlkView.TrendType method plus is a built-in method % Shadowed RecordBlkView.UpdateMode method plus is a built-in method % Shadowed RecordBlkView.Visibility method plus is a built-in method % Shadowed Streamout.ExcelTime method plus is a built-in method % Shadowed dastudio_util.cooperative.AsyncFunctionRepeaterTask.Status method plus is a built-in method % Shadowed mf.zero.meta.Language method plus is a built-in method % Shadowed matlab.unittest.internal.fixtures.FolderScope method plus is a built-in method % Shadowed matlab.unittest.Verbosity method /MATLAB/toolbox/matlab/testframework/unittest/core/+matlab/+unittest/+internal/+diagnostics/FormattableString.m % Shadowed matlab.unittest.internal.diagnostics.FormattableString method plus is a built-in method % Shadowed matlab.internal.timer.CallBackTypeEnum method plus is a built-in method % Shadowed matlab.internal.timer.BusyModeEnum method plus is a built-in method % Shadowed matlab.internal.timer.ExecutionModeEnum method plus is a built-in method % Shadowed multimedia.internal.audio.file.FrameSize method /MATLAB/toolbox/matlab/strfun/@string/plus.m % Shadowed string method /MATLAB/toolbox/matlab/bigdata/@tall/plus.m % Shadowed tall method /MATLAB/toolbox/matlab/datatypes/calendarDuration/@calendarDuration/plus.m % Shadowed calendarDuration method /MATLAB/toolbox/slcoverage/@cvdata/plus.m % Shadowed cvdata method /MATLAB/toolbox/control/ctrlmodels/@InputOutputModel/plus.m % Shadowed InputOutputModel method /MATLAB/toolbox/wavelet/wavelet/@laurpoly/plus.m % Shadowed laurpoly method /MATLAB/toolbox/wavelet/wavelet/@laurmat/plus.m % Shadowed laurmat method /MATLAB/toolbox/nnet/deep/@dlarray/plus.m % Shadowed dlarray method /MATLAB/toolbox/robust/rctobsolete/robust/@icsignal/plus.m % Shadowed icsignal method /MATLAB/toolbox/finance/ftseries/@fints/plus.m % Shadowed fints method /MATLAB/toolbox/parallel/parallel/@codistributed/plus.m % Shadowed codistributed method /MATLAB/toolbox/parallel/gpu/@gpuArray/plus.m % Shadowed gpuArray method /MATLAB/toolbox/econ/econ/@LagOp/plus.m % Shadowed LagOp method /MATLAB/toolbox/coder/float2fixed/mathlib/@emlhalf/plus.p % Shadowed emlhalf method /MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % Shadowed sym method
Why should your function operating on 'plus' state that the inputs have to be an integer type? Why not showing that it could accept a datetime array, a duration array, a sym array, an InputOutputModel, etc.?
which -all plus(sym(1), 2) % This would call the sym method
/MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % sym method
which -all plus(hours(1), datetime('now')) % This would call the datetime method
/MATLAB/toolbox/matlab/datatypes/datetime/@datetime/plus.m % datetime method
  4 Kommentare
Steven Lord
Steven Lord am 25 Mai 2021
Two parts of that aren't too difficult, at least for functions in files with the .m extension. It would be more difficult for functions in files with the .mlx extension. You'd have to use a different technique for classes defined in files with the .m extension.
For functions in .m files read the file line by line until you get to the function declaration, which starts with the function keyword.
Parse that declaration to obtain the name of the main function (though if it differs from the name of the file, you would have to call that function using the file name rather than the name in the declaration) and the names of the input and output arguments.
MATLAB is not a strongly typed language and so if it is possible to determine what types the function can accept and process it still would be difficult. If the function uses an arguments block it may be possible to parse. But unless you do that I could call your add function with a sym array as input even if you'd intended it to only accept integer arrays. In that scenario MATLAB would call your add function, but whether or not that operation would complete sucessfully would depend on how much input argument validation the author of the add function included in the code.
If you're trying to find some of this information about classes defined in a file that starts with the classdef keyword you may find the metaclass functionality useful.
But what are you planning to do with this information on the server? Are you trying to automate some sort of Doxygen-like documentation? Are you trying to use this as part of check-in messages in an SCM system like Git or Subversion?
John D'Errico
John D'Errico am 25 Mai 2021
You are still going to have difficulties.
When you write a function, I'll call it myfun, there is not any place in MATLAB where you specify what class of variable may be passed in. The header in your function has no such place for that information. As well, knowing the internal name of a variable is irrelevant. You can pass in anything you want.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by