Debugging function code and workspace variable issues

21 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 2 Jan 2015
Edited: Stephen23 on 2 Jan 2015
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.
  1 Comment
Stephen23
Stephen23 on 2 Jan 2015
Actually this works:
function temp(x)
disp(size(x))
end
Save and set break-point on the second line. Then call it from the command line:
>> temp(1:3)
>> dbup
>> global y
>> y = 3;
>> dbdown
>> global y
Bingo! Variable y is now available in the function workspace. But honestly, it would be easier just adding a new input and calling the function again...

Sign in to comment.

More Answers (0)

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!