MATLAB: Debugging function code and workspace variable issues

debugging

Hi all,
Relatively new MATLAB user and I've written a couple of functions and have started debugging those functions. I'm a bit confused though by the behavior in debug mode compared to what I'm used to when debugging script files.
For example, when running my function code up to a breakpoint, MATLAB clears the workspace (or more accurately it looks like MATLAB changes the workspace from the general workspace to the function's workspace). If I need some of the variables from my general workspace as input arguments to the function, MATLAB won't let me copy those variables over to the function workspace after function debugging has started.
How do folks work around these issues (or am I doing something wrong here)?
Thanks,
JK

Best Answer

I am not sure if you can (easily) grab values from one workspace and pull them into the current function workspace... you might like to try dbup , which lets you change workspace while debugging. But variables only exist in one workspace, unless explicitly specified otherwise (e.g. global). It might be possible to do some major hackery using assignin and evalin and the like, or perhaps even calling global, but this would not be beautiful...
You are correct that when using the debugger (on a function) the workspace shown in the IDE changes from the "base" workspace to that of the (normally hidden) function itself. All of the variables that you have defined within the function are shown there, including the ones that are inputs to the function. Thus the standard way to get variables from the base workspace into a function workspace is to call that function with those variables as input arguments, as the workspaces are kept quite separate.
This page covers all of this in much more detail:
Note that scripts do not have their own workspace, as they operate in the base workspace.
Anyway, my answer for what it is worth: the simplest solution would be to change the function by adding a new argument, and then run it again.