Let's imagine that you are dividing your script by parts using functions. Every one has its own workspace. Is there any way to switch between workspaces and directly have a look at its content? It's just like we do with the base workspace.
If you only want to inspect different (read: parent) workspaces, you can do that while debugging using the "Function Call Stack" menu as shown below:
Each of the displayed lines (in this example: updateSourceImageAndStats, loadCallback, Base) represents a different workspace, and switching between them will show you the contents of the workspace for that level. Also consider dbstack and related functions.
If however you want to execute code in some parent workspace, you can use evalin.
Related
In Simulink, certain error messages provide clickable links to the origin of the error.
See, for example, the following error:
If I click one of the three links in the message, say, Parent setting, Simulink opens the code generation settings dialog and indicates the specific setting(s) using a blue border:
As a user, can I achieve the same highlighting programmatically (using a .m-script)? If so, how?
Yes, this is possible, but it requires the use of Simulink internal support functions. Their API is undocumented and may therefore be unstable. Use at your own risk.
The highlighting in the question is achieved as follows:
slprivate('modelref_highlight_configset_setting', 'rtwdemo_capi', 'RTWCAPISignals')
Here,
slprivate is a .m-function that ships with Simulink. There is no help entry for the function. Its only mention in the help is on the 'Set Simulink Preferences' help page. Its implementation is simple: it is a wrapper around feval. The implementation can be opened by executing the command >> edit slprivate from the Matlab Command window.
The function's documentation is as follows:
slprivate is a gateway for internal support functions used by Simulink.
VARARGOUT = slprivate('FUNCTION_NAME', VARARGIN)
In the usage above, the first parameter 'modelref_highlight_configset_setting', is the Simulink internal support function. In this case, it is the function that does the highlighting.
The second parameter 'rtwdemo_capi', is the name of the Simulink model whose Configuration Parameters window should be opened for highlighting.
The third parameter, 'RTWCAPISignals', is the name of the configuration option to highlight. In this case, that single option highlights two UI elements.
Names of configuration options can be found as follows:
Open the model's Configuration Parameters window (e.g. in Simulink: menu Simulation -> Model Configuration Parameters, or Ctrl + E)
Right-click on (or, rather, next to) the option
In the context menu that appears, click What's This?
In the help window that appears, scroll down to Command-Line Information. The name of the option is given in the Parameter field.
Edit:
The way I figured this out may be useful for other internal functions, so I'll leave that here as well. If the build is started from the Command Window (>> rtwbuild('rtwdemo_capi')) instead of from the GUI, warning and error messages are printed to the command window as well, including the clickable links. If one then hovers over such a link with the mouse pointer, the corresponding command is shown in the Matlab status bar (at the bottom of the main Matlab window).
I encountered an annoying problem with Matlab 2016b: I cannot run folded code sections separately. More specifically, in Matlab editor buttons "Run and Advance" ans "Run Section" appear grey (unclickable) and Ctrl+Enter to run a code section does not work either.
My script does recognize folded code sections (separated with %%) just fine and I am able to use the "Advance" button to move between folded sections, so it seems the problem is not some syntax error hidden in the code. I am also able to run the script in the usual way with "Run" and even run selected lines of code with F9 command. I would, however, want to avoid using the latter option as "Run Section" and "Run and Advance" options are quite handy and do not require highlighting lines of code.
Any ideas what could cause the problem?
The root of the problem was my auxiliary functions defined within the main script. Since these functions were located in one of the sections, I couldn't call them from other sections separately. The solution was to save auxiliary functions as separate m-files. The following image highlights the problem:
For example: Rstudio uses so called projects - text files ending with .Rproj. When you click on a project file, it opens up Rstudio and sets working dir to where the project file is. Optionally, it executes any code written it. However, it does not open itself (i.e., it does not show up in the script editor).
Is there something like that in Matlab? If not, how to emulate it?
Currently I use to make an .m file with cd, addpath calls etc. But when I click in the file browser:
it just opens Matlab and shows up in the script editor without running
opening Matlab is what I want, but showing up in the script editor is actually redundant; I only need to run it (and use the results in my Matlab desktop session)
(What I want to avoid is having to open the script file, run it manually and then having to close it again. It is annoying!)- edited
Matlab does not have "project files" (as far as I know).
However, I think you can easily emulate what you want.
Let's suppose you have your code in a folder C:\MyProject:
1) Create a new m-file C:\MyProject\MyProject.m with all your initialization code (cd, addpath calls, global variables, whatever you need).
Here's a simple example for demonstration purposes:
disp('Replace this with your initialization code');
2) Create a batch-file C:\MyProject\MyProject.bat as follows:
MATLAB -r "run MyProject"
Now, by double-clicking the batch-file you will:
open the complete Matlab environment
execute the script MyProject.m (without loading it in the script editor)
For this purpose, MATLAB offers startup.m files (online documentation).
You have to put all your initialization code in a file called startup.m, which needs to be located within the MATLAB search path (i.e. within your project folder). The script will be executed every time you open MATLAB by double-clicking a arbitrary m-file from your project folder.
I am writing a GUI program with matlab and I would like to test my variables to see if they are properly imported and to test how to access to different parts of the variables imported so I want my variables to be shown in workspace window of Matlab desktop
the main trouble is if I write a common program in the mfile editor after running the program variables will be shown in workspace windows but about GUI programs its not true
And if I Save the workspace of my program from the path
File>Save Workspace As...
in MFile Editor and then I try to open this access file in desktop i encounter
No variables created
How can I accesss the workspace of my GUI?
If it is just for inspection, the easiest way is to use the debugger: set a breakpoint in one of the GUI callbacks, execution of the code halts there, and allows you to inspect the workspace, among other things.
If you want the GUI to return data to the main workspace, you add the line uiwait(hObject) to the end of the opening function. Then, the callback to e.g. the OK-button should have a line handles.Output=myData; guidata(hObject,handles); to send the variable myData to the GUI output, followed by uiresume(hObject). This way, you can call your GUI as myData = myGUI;, and myData in the base workspace will be filled with whatever data the OK-callback gives it once the OK-button is clicked.
Note: Functions assign outputs, not internal variables to the base workspace. So I guess what you describe as "functions in the editor" are actually scripts that access and modify the contents of the workspace from which they're called.
Intellij has a shortcut for moving the current method internally in the class ( I think it is Ctrl+Shift+{Up or Down Arrow})
I would like to know if Eclipse has the same functionality.
I could mark the whole method and then alt-arrow to move it, but then it would move one line at a time. I would like it to move one member for every keystroke.
I would also not like to use the mouse (in the outline view where one may re-order members of a class)
if you double-click just after the opening { of a function, you will select the all function body.
You can then:
Alt+Up: to move the function up one line
Alt+Down: to move the function down one line
Of course, that is the shortcut you describe in your question, and moving a function by just one line can result in an incorrect code (as the function can end up within another one).
You have to repeat the shortcut the appropriate number of time to actually move the function before or after another one.
So the answer is: No, not exactly like IDEA, but close enough.
Actually, if the outline view is not sorted alphabetically, you can drag and drop a function in order to reorder them.
But that is not a shortcut.
If you can afford to use the mouse, you can do it from the outline view. That will be easiest.