MATLAB Answers

nested function call using inputname crashes matlab

1 view (last 30 days)
Milind on 8 Jan 2015
Edited: Milind on 9 Jan 2015
I'm trying to develop an efficient pipeline execution framework : more details here
Because of the fact that there are no pointers in matlab, I am relying on inputname() for grabbing the input, output variable names, and function names, along with using f handles. I'm experimenting with different (command line) user interfaces for this purpose, and currently settled on this :
a = intel_pipe;
here i1,i2 are input variables, o1,o2 are output variables, and f1 is a function.
function i_vars = i(obj,varargin)
i_vars = obj.add_vars(args);
function o_vars = o(obj,varargin)
o_vars = obj.add_vars(args);
exec_comm_script.m :
strerror = 'arguments cannot be expressions';
nvarargs = length(varargin);
nconstargs = 1;
args = zeros(1,nvarargs);
for ii=1:nvarargs
if isempty(varn)
When I execute this, it works fine if debugging, but crashes unfailingly if not debugging. When, however, I paste the contents of the script into both the functions, (duplicating the content, it works fine.
What's going on?
Note : As expected I am using evalin and assignin here, but I really don't see any way to do this kind of thing other than to use matlab's reflection abilities. The important thing here is that this is meant to be used with large data that takes time to compute. Hence I cannot afford to keep copies of data, and hence evalin('base','...'); is a must.
I am also checking for empty names and disallowing expressions in the input and output arguments because it defeats the purpose of deferring execution in input, and is invalid anyway in the output.
You could say I should be asking the users to enter variable name strings in the first place, but then it would become my responsibility to check if the variable exists etc, instead of the interpreter's. Plus it's really ugly and irritating for the user.


Sign in to comment.

Accepted Answer

Philip Borghesani
Philip Borghesani on 8 Jan 2015
You have found a bug in the MATLAB jit. This code works correctly if you enter feature accel off from the command line first. It will be fixed in a future version of MATLAB.
That said this technique is a bad idea and should not be needed. By utilizing of MATLAB's copy on write behavior and in-place optimization (see Loren's Blog) it should be possible to avoid unnecessary copies of large variables.
If you publish code samples that seem to be too expensive to do without evaling in base I expect contributors here can assist in specific optimizations that will not require use of inputname and evalin.
A second reason to avoid inputname is that it can be a very slow function because it must introspect into the currently executing stack frame.

  1 Comment

Milind on 9 Jan 2015
Thanks, that worked. I did not notice any large increases in running time, but requires more testing with longer-running functions.
I did consider using the copy-on-write feature, but how would I detect updates to variables?

Sign in to comment.

More Answers (2)

Sean de Wolski
Sean de Wolski on 8 Jan 2015
If the crash is not happening in debug mode, this implies that somewhere an event is not being flushed properly. Add a drawnow in there in a few places, this is implicitly called when you're stopping in debug mode.

  1 Comment

Milind on 8 Jan 2015
Interesting, did not know that. Didn't work however, even though I added a drawnow the moment I land in a.f();
Will keep tinkering with it though.

Sign in to comment.

Adam on 8 Jan 2015
'This command can be used only inside the body of a function.'
from the help page for inputname:
doc inputname
I'm not sure about the rest of what you are doing, but that function takes input names within the scope of a function and the names are those of the variables in function scope, not the names of the variables in the workspace from which they were passed in which is something you should never be wanting to get hold of anyway.


Milind on 8 Jan 2015
That directly contradicts what I keep seeing in my usage : inputname(n) gives me the name of the nth input argument in the caller's scope.
And if not that, how would I do what I'm trying to do here?
Adam on 8 Jan 2015
Ah yes, that is true actually. I didn't read that far through the help and assumed there wouldn't be a function to do that.
I guess you can use it for that then in a function though it sounds like dodgy programming practice to be interrogating the names of variables in the calling workspace. They should never be the business of a function that is being called.
Titus Edelhofer
Titus Edelhofer on 8 Jan 2015
Generally speaking: you are right. inputname is usually interesting when writing a display method for your own objects (classes). The output of a display then is e.g.
A =
That's where the "A = " might add some "niceness". It's usually not used to rely on it in the sense that you work with variables (by name) from the calling workspace.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!