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

조회 수: 4 (최근 30일)
Andres
Andres 2023년 11월 24일
댓글: Andres 2023년 11월 25일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2023년 11월 24일
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
Walter Roberson
Walter Roberson 2023년 11월 25일
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.
Andres
Andres 2023년 11월 25일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by