How to enable Editor data tips when editing functions (not scripts, not debugging)?

Once activated in Preferences/Editor/Display (in my R2023b Matlab installation), Editor data tips show up in scripts
and while debugging functions
as desired, but they do not show up when editing functions
According to https://www.mathworks.com/help/matlab/matlab_env/about-editor-debugger-preferences.html the data tips should appear "when you are editing a MATLAB code file".
I understand that in many cases data tips showing base workspace values might not be useful when editing functions, but sometimes it is convenient e.g. to quickly run code snippets from a function and monitor variables.
Is there an option to enable Editor data tips while editing functions? Has data tip behaviour changed in recent Matlab releases?

Answers (1)

If you are not executing a function, then it has no workspace to hold values, so data tips inside the editted function would not make sense (except perhaps global variables or persistent variables.)
Likewise, after you have returned from a function, it's workspace has been destroyed and there is nothing to take the datatip of.
When you edit scripts, MATLAB assumes for datatip purposes that you will be evaluating the script in the base workspace, so it can reasonably datatip variables that are in the base workspace.

3 Comments

I'm aware of the difference in workspaces. As I wrote, you could deliberately create the required variables in base workspace and run some code of the function - in base workspace of course, using "Evaluate selection", "Run section", ... - to monitor what it does, just as you would do when you edit a script. That could be just as helpful, couldn't it? So it would be nice to have that option and it should be a mere Editor setting, with the same assumptions for data tip purposes as in scripts.
Actually, I wonder if data tips did show up like that in functions in earlier releases but I can't check right now.
Consider
function take5
%%
foo = 123;
%%
take6
function take6
disp(foo)
end
end
If you Run and Advance while pointing to the assignment to foo, then MATLAB runs the code and foo gets created in the base workspace.
If you then Run and Advance the next section, with the call to take6, then MATLAB complains inside take6 about foo not being defined.
When you extract that subset of the code into a script, then the foo inside take6 is no longer a reference to a nested variable. Inside scripts, functions do not search the base workspace looking for variables. Nested variables are only searched for when you have nested functions.
Now, if you have something like
function take7
%%
foo = 123;
%%
take6(foo)
function take6(foo)
disp(foo)
end
end
and you Run and Advance each part, then foo is first written into the base workspace, and then the second Run and Advance is running a script that can find foo in the base workspace to pass to the function, so a sensible result can be obtained (at the expense of clobbering whatever foo was in the base workspace.)
And it is true that if you go through that kind of work, that datatips are not available in the editor while you are executing by section.
I don't know... I think if I were in that kind of situation... well, I would probably be running under debugging instead. Or at worst I would comment out the function header so that it all became a script.
Yes, that's what I showed in the first two pictures. You could also copy the code in question to another Editor tab, then you would still be able to call the function, albeit not with your current code modifications.
All of this is just slightly less convenient (and not in line with what the Editor data tip preferences documentation says, see original post), imho.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 24 Nov 2023

Commented:

on 25 Nov 2023

Community Treasure Hunt

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

Start Hunting!