closing specific file from Matlab editor - matlab

Is there's any command line interface to close a single file from all the open files?
I know I can close all files yet I sometimes work from a network location and I would like to close a set of files with a single script.

For recent MATLABs you can close files that are open in the MATLAB editor with something like the following:
matlab.desktop.editor.findOpenDocument( 'C:\my_function.m' ).close()
For older MATLABs you can try something like this (but I don't have an old installation to test):
com.mathworks.mlservices.MLEditorServices.closeDocument( 'C:\my_function.m' )

Related

MATLAB File Association in Visual Studio 2015 / TFS

I'm using TFS in Visual Studio 2015, and have a question about MATLAB file associations. For .m files, there is no association to MATLAB, as seen here:
Confusingly, there is an association for .fig and .mat files, so VS must recognise that MATLAB "exists". If I double click one of these file types, it opens it in MATLAB as it would from within an Explorer window. This is the behaviour I want for .m files.
As a minimum, I'd like to have the MATLAB icon for .m files so that they're easy to spot in a directory. As an ideal solution, these files would also open in MATLAB (not the VS text editor) from TFS.
I have tried the "File Extension" option inside of Options > Text Editor, but MATLAB isn't an option for me in the Editor list.
It's worth noting that my work network is pretty strict on software installs, so the fewer external add-ins the better (ideally none). Suggestions appreciated.
There's a utility that I often use to fix OS-level association problems with MATLAB-related files - associateFiles from FEX.
You should probably call it with associateFiles('deleteadd','.m'), which would generate a registry file named MatlabFileAssocFix.reg that has the following contents (obviously the MATLAB path is generated according to your system):
Windows Registry Editor Version 5.00
;FIXES MATLAB FILE ASSOCIATIONS
;REMOVES M FILE ASSOCIATIONS
[-HKEY_CLASSES_ROOT\.m]
[-HKEY_CLASSES_ROOT\MATLAB.m.9.1.0]
[-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m]
;ADD SHELL OPEN
[HKEY_CLASSES_ROOT\Applications\MATLAB.exe\shell\open\command]
#="\"C:\\D\\Program Files\\MATLAB\\R2016b\\bin\\win64\\MATLAB.exe\" \"%1\""
;ADD M FILE ASSOCIATIONS
[HKEY_CLASSES_ROOT\.m]
#="MATLAB.m.9.1.0"
"Content Type"="text/plain"
"PerceivedType"="Text"
[HKEY_CLASSES_ROOT\.m\OpenWithProgids]
"MATLAB.m.9.1.0"=""
[HKEY_CLASSES_ROOT\.m\PersistentHandler]
#="{5e941d80-bf96-11cd-b579-08002b30bfeb}"
[HKEY_CLASSES_ROOT\.m\Versions\MATLAB.m.9.1.0]
"FileVersionMS"=dword:00090001
"FileVersionLS"=dword:00000000
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0]
#="MATLAB Code"
"FriendlyTypeName"="#C:\\D\\Program Files\\MATLAB\\R2016b\\bin\\win64\\matlab.exe,-58"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\DefaultIcon]
#="C:\\D\\Program Files\\MATLAB\\R2016b\\bin\\win64\\m.ico,0"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Open]
#="Open"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Open\command]
#="\"C:\\D\\Program Files\\MATLAB\\R2016b\\bin\\win64\\matlab.exe\""
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Open\ddeexec]
#="uiopen('%1',1)"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Open\ddeexec\application]
#="ShellVerbs.Matlab.9.1.0"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Open\ddeexec\topic]
#="system"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Run]
#="Run"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Run\command]
#="\"C:\\D\\Program Files\\MATLAB\\R2016b\\bin\\win64\\matlab.exe\""
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Run\ddeexec]
#="run('%1')"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Run\ddeexec\application]
#="ShellVerbs.Matlab.9.1.0"
[HKEY_CLASSES_ROOT\MATLAB.m.9.1.0\Shell\Run\ddeexec\topic]
#="system"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m\OpenWithProgids]
"m_auto_file"=hex(0):
"MATLAB.m.9.1.0"=hex(0):
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m\OpenWithList]
"a"="MATLAB.exe"
"MRUList"="a"
When you import this file, it deletes whichever association exists for .m and associates it with MATLAB.
I don't know if this is applicable to your problem, but I thought it should be mentioned.
So after a long time, I've found a reasonable workaround for this.
You can set the "Open With" default for .m files, but this seems to open a new instance of MATLAB for every file - not ideal! Suggested by SACn below their answer.
A better option is to use Visual Studio's "External Tools".
Go to Tools > External Tools, then as this documentation shows, create a new tool. Use the following parameters:
Title: Your Title Here
Command: cmd.exe
Arguments: /c "$(ItemPath)"
Tick "Close on exit". This opens the command prompt with the /c (terminate after command) flag, and runs the file name of the file which you have open in VS. On Windows systems, this will open the file in an open instance of MATLAB, or launch a new one if none exist.
This can be run many ways... Assign a keyboard shortcut, run from the Tools menu, add it to the Toolbar, add it to the context menu.
Now there is one click / key press to launch an open .m file as desired. It still lacks the MATLAB file association (and corresponding icon in the file browser) but does the main job!
Be advised: MATLAB is written in Java and not based on NET or Visual Studio extensions so if we're trying to open MATLAB Editor as plugin this is not a good idea for now.
Now to edit .m files you've to define new (external) default editor for a file type.
And if MATLAB license is not present use Notepad++, Sublime and other free editors which support .m file syntax highlighting.
If I understand correctly, you want to integrate MATLAB with VS (It seems your issue is not TFS related).
There is not such extension for Visual Studio, but there is a MatLab extension for Visual Studio Code.
=========================================================================
From the comment, it seems you want to use team explore in MATLAB to enable integrated use of Team Foundation Version Control. I'm afraid your requirement can't be achieved at this moment. MATLAB doesn't support team explorer or TFS MSSCCI Provider.
I have submitted a user voice at website below, you can vote it:
https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/18325612-enable-integrated-use-of-team-foundation-version-c.
OK, here's how I did it:
I have VS code installed, and as such, it creates an association in my default programs like this:
Then you can just point your VS program at this extension:

Does Matlab have "project files"?

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.

Is it possible to undo close tab in Matlab's editor?

Many web browsers allow to undo close tab:
Is it possible to undo close tab in Matlab's editor?
I use R2014a on Windows 7.
It isn't officially supported in MATLAB, but with reference to Undocumented MATLAB, there is a workaround.
MATLAB stores its Desktop windows state in a file called MATLABDesktop.xml that is located in the user’s prefdir (preference directory) folder. You can echo where this is in MATLAB by simply typing in prefdir in the Command Prompt and pushing Enter or RETURN. As you are also using Windows, you can also do this to open up the folder within MATLAB:
winopen(prefdir);
This should open up a new Windows Explorer window that will directly bring you to the prefdir folder.
This file includes information about the position and state of every Desktop window. Since the editor files are considered Desktop documents, they are also included in this file. As such, when you close the editor, these documents are simply removed from the MATLABDesktop.xml file.
It turns out that MATLAB automatically stores a backup version of this file in another file called MATLABDesktop.xml.prev, in the same prefdir folder. I'm also using MATLAB R2014a in Windows 7 and I have double-checked to see if these files are also on my system and they are!
So before doing anything else, close MATLAB, delete the latest MATLABDesktop.xml file, replace it with a copy of the MATLABDesktop.xml.prev file, renaming it to MATLABDesktop.xml. After, restart MATLAB and the editor should reopen with your previous tabs.
You may use the following code. It extracts the file names from MATLABDesktop.xml.prev
%parse XML file
xmlFiles = xmlread([prefdir filesep 'MATLABDesktop.xml.prev']);
%Retrieve the "clients"
FileNodes = xmlFiles.getElementsByTagName('Client');
%get the length of the FileNodes
nrFiles = FileNodes.getLength;
%initialize Files
Files = cell(nrFiles,1);
%initialize isFile
isFile = zeros(nrFiles,1);
%Iterate over all Elements and check if it is a file.
for iNode = 0:nrFiles-1 % Java indexing.
%Files
Files{iNode+1} = char(FileNodes.item(iNode).getAttribute('Title'));
%check if the "client" is a file:
isFile(iNode+1) = exist(Files{iNode+1},'file') == 2 && ~(strcmp(Files{iNode+1},'Workspace'));
end
%remove the other files:
MyFiles = Files(find(isFile));
%open the files in the editor:
edit(MyFiles{:});
From mathworks

Using vim as default editor for matlab

I want to use gvim as the standard editor for Matlab. It used to work on Linux but now I am forced to use windows and I can't seem to figure out how to set the editor such that files are opened in gvim in a new tab.
In the preferences there is a field which allows to pass a command that points to the prefered text editor. That works, but things fail when I try to give additional options, in my case that would be "--remote-tab-silent" to tell gvim to open the file in a running instance in a new tab. More specifically, the following line in the matlab preferences works:
C:\pathtovim\gvim.exe
while this one fails
C:\pathtovim\gvim.exe --remote-tab-silent
A command line opens with the following error message (my own translation from German):
The command ""C:\pathtovim\gvim.exe --remote-tab-silent"" is either spelled incorrectly or could not be found.
My guess is that it has something to do with the additional quotes, I have no idea why the command is issued with quotes, even though in the field I put it without. The follwing commands work when typed into the command line directly:
"C:\pathtovim\gvim.exe"
C:\pathtovim\gvim.exe --remote-tab-silent file.m
and this one fails:
"C:\pathtovim\gvim.exe --remote-tab-silent file.m"
I'd really appreciate any help! Thanks!
I can't find a good way to hack around it through the MATLAB settings; it looks like MATLAB is stupidly expecting the text editor to take only file names as arguments.
I think your best option, is to create a .bat script that simply passes any arguments it receives on to Vim, inserting the --remote-tab-silent.
I.e. create a .bat file with these contents:
"C:\pathtovim\gvim.exe" --remote-tab-silent %*
Then set up your MATLAB preferences to invoke the .bat file rather than Vim.

Printing a Project in NetBeans

I have an exam and I would like to print my Java (in NetBeans project) files as fast as possible. I was wondering is there a way to print an entire project/project in NetBeans with one click? or do you have a better suggestion? (I will be using Windows XP and there is no nice editor like Gedit or Kate installed)
Give that the identical question on the netbeans forum from last year is not answered yet, I would say no.
When I needed to print off my final year Comp Sc project (needed to hand in a full print out + polished install CD, oh the poor trees that died for that one...), I opened every file then went through and did a print & close on each one. :(
It can be done. Switch to file mode, select all files, open all, then print. From http://dgreen.github.io/blog/2010/11/02/printing-multiple-files-in-netbeans/
I had the same problem. I am in a Computer Science class with Java Programming. My homework asignment involves creating a project with many class files (7 at the moment) so I searched google for how to print out the project and found nothing. However, I have come up with a bit of a kludgy way to do this, after reading it you can be the judge of how much time it will actually save. The more classes you have in a single project, the more time it will save. The methodology is kludgy because it involves as little system access as possible, because as you said you are on a school computer so I will assume only the most basic access. It DOES require command prompt access, so this may not be helpful for you. However, it is possible that if you cannot open the command prompt you may be able to execute a .bat file. So here goes.
Point command prompt to your source directory and type:
dir /b > projectDir.txt
or save that 1 line in a text editor as dir.bat
this will give you a text file listing all of the java files in your source directory
open notepad and type this 1 line
/*PAGEBREAK*/
save that file as pagebreak.txt
now, open the projectDir.txt file you created with Notepad.
do a global replace of ".java" with ".java+pagebreak.txt+"
get rid of the line that has projectDir.txt listed
save the file as projectDir.html
open this file in a web browser. the point of this is to get rid of the line breaks created by using dir /b.
now, copy this line of text and paste it into a new notepad window. at the beginning of the line, add "copy /b " and at the end of the line, replace the final "+" with " wholeProject.java"
run your bat file. if everything has been successful, you will have a single java file containing all of your classes. add this file to your project tree in netBeans IDE.
Export that file as an HTML file. one problem is that if you use line numbers, the numbers will be continuous; each new class will not start with 1, so I would advise against that.
Open up the html file in notepad. Do a global replace of "/*PAGEBREAK*/" with "<p style='page-break-before: always'>". Save your file, open it in a browser.
When you print it, it should print each class on its own page.
hope this helps! netBeans should really implement this though, my method is pretty goofy and contrived.
I found a new way (I hope it is a new way...)
You first want to copy the html , you can do that by choosing "Print to HTML ... " option from the Netbeans settings and choose the " Print to Clipboard " option .
Open the any text app and paste it , save with .html extension .
Now you can use from any browser to print the way you want it .