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

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

Related

Start App from file storing settings and load them at startup

I'm working on an App in app designer. Within the app the user will select a bunch of options before running some calculations.
To simplify this process I added a "Save as..." menu so that the user can save the current settings to a file (.mat) and reload them when they open the app the next time.
What I'm trying to achieve is that the user can double click on the previously saved .mat file, which will launch the app and the app will automatically read the double clicked file and load all the settings.
All this needs to happen after the app is compiled and distributed as an executable.
I'm thinking that one way to achieve this is to make a startup window of the app that calls the main window passing the file path as parameter.
Any suggestion would be really appreciated.
Hi, I think I may have a fairly simple, albeit involved, solution for you.
Brief solution overview (TL;DR)
Save the settings from the app with an extension other than .mat, e.g. .mydat. Add an App Input Argument and have the startupFcn treat the argument as a file name to a *.mydat file and be sure to also handle the case that the argument is left out. After the first output file is saved, use windows Open with... to select your app. Now double clicking the *.mydat file will open your app's .exe and will provide the file name of the clicked file to the input argument in your startupFcn.
An example in MATLAB 2018a as a compiled exe on windows 10.
Ok, to start. Let's setup a simple app (I called it runAppFromData) that takes a string input to an edit field and saves it in a file called 'settingsValues.mydat'. It looks like:
The callback for the Save button collects the Value into a local variable called value and then saves it to disk:
% Button pushed function: Save
function save(app, event)
value = app.InputField.Value;%#ok
% User selects save location
saveLocation = uigetdir();
% Now just save the value variable to the selected location.
save(fullfile(saveLocation,'settingsValues.mydat'), 'value', '-mat');
end
I don't know when appdesigner added the feature to "run app with inputs" but I have it with 2018a:
We make a single input, fileName that expects a file name as a string (you'll see why below). So add the input and click OK. Then we're sent to "code view" at the startupFcn. Here we'll write the logic that parses the input file. For my simple example app, I load the input file into a struct and then send the value to the edit field:
% Code that executes after component creation
function startupFcn(app, fileName)
if nargin < 2 % app is itself an argument
% just continue running the application without error
return
end
% fileName is a string, so let's load it into a struc
S = load(fileName, '-mat');
% The value field will be there because that is how we wrote it
app.InputField.Value = S.value;
end
Note, I performed a nargin check to handle the first-run case (and anytime the app is run from the actual executable).
MATLAB doesn't care what the file extension is of a matlab file and if you have an unknown file extension, e.g. .mydata, double-clicking the file in windows will ask you to choose the application, which works to your benefit for deployment:
A couple things to consider.
When the app is opened from the .exe it will always show the default values. If you want to input some other default values you can edit your windows shortcut Target field to supply a file path for the desired input file (see here). This saves recompiling with new defaults, but the file has to remain somewhere (you can package it with the app too).
Sorry this answer got soo long! I hope it helps!
You can't double click a .mat file and open an entire executable, but you can definitely add a startup function that asks you to open a .mat file. My suggestion though would be to make sure that you have a template file at least in place, so that the user doesn't run into problems the first time running the program where there is no file to open.

closing specific file from Matlab editor

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' )

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.

Where is startup.m supposed to be?

I've been chasing the answer to this question in the MATLAB documentation for a long time...
For example, at the bottom of
http://www.mathworks.com/help/matlab/matlab_env/changing-the-startup-folder.html
it says
Use the startup.m file to specify the startup folder...
...which is plainly absurd, since elsewhere the documentation says that startup.m is to be found in the so-called "startup folder". So therefore there's no way for this file to specify where this folder should be.
Etc., etc., etc. This sort of circularity pervades everything I've found in the docs on startup.m.
What I want to know is: can I or can't I customize the location of the "startup folder" in a way that is persistent, and if so, where is this persistent information stored?
The best method, I find, is this. Let's say you want MATLAB to start up in mystartupdir, and you've placed startup.m in that directory.
On Windows, make a shortcut icon to MATLAB, then right-click on it and select Properties. Edit the field Start In. Now, use this icon whenever you want to start MATLAB.
On other platforms, you can run MATLAB with the -sd flag to specify the startup directory:
matlab -sd mystartupdir
If you don't specify a startup directory, MATLAB will use the default specified by the userpath command. You can place your startup.m file there.
According to this page in the docs, you should create your own startup.m in the "startup directory" which, if you follow the link, leads to this page explaining the definition of "startup directory" in this context.
You can retrieve this "startup directory" with the userpath function which returns, on my system:
>> userpath
ans =
C:\Users\MYUSERNAME\Documents\MATLAB;
Personally, I just set the "Start In" in the shortcut to whatever I want - but obviously this won't work if you're not on Windows; if you're not on Windows or prefer not to rely on the shortcut you should create a startup.m with a call to cd in whatever directory userpath returns.
The docs also say you can modify the userpath function, if you so desire, or the matlabrc.m file in matlabroot/toolbox/local (but you can only do the latter if you're a MathWorks engineer or a system administrator, otherwise MathWorks will rain fiery hell down on you from above, or something...).
On Mac OS X, you may put your startup.m file in /matlabrootfolder/toolbox/local/. For example, this path might look like the following for Matlab 2012: /Applications/MATLAB_R2012a.app/toolbox/local/.
To prevent the file from being removed after upgrading Matlab, you may use a symlink to the file. If your startup file is stored at $HOME/myDir/startup.m, for example (in Bash):
cd /Applications/MATLAB_R2012a.app/toolbox/local/
ln -s ~/myDir/startup.m .
Reference: http://www.mathworks.com/help/matlab/matlab_env/startup-options.html
This is meant as a pointer for Lx users (as confused as I was at first...). I work on Debian based boxes, but the same should apply to the CentOS system lineup, etc.
Check that after install you do have the directory/ies: ~/.matlab/<yr_release(s)>
Note that you may have several releases there as is my case. matlab actually permits that.
Create ~/.matlab/startup.m Incidentally I also created my directory Workspace there. That's completely optional
In your Matlab window's Home tab find the menu iten/icon Set Path, click on it and add a "matlab search path" with the button Add folder top left. Add ~/.matlab as a new search path. Change should take effect immediately.
Restart Matlab to check that the content of ~/.matlab/startup.m is correctly taken into account at launch time.
I hate GUIs... I thought you might want to know ;-) HTH.
As read in http://fr.mathworks.com/help/matlab/matlab_env/matlab-startup-folder.html#buj_13n :
Changing the Startup Folder
Starting in R2014b, you can change the startup folder using the
General Preferences panel. On the Home tab, in the Environment
section, click Preferences. Select MATLAB > General. Choose an option
for the Initial working folder.
By default, the initial working folder is set to Location based on
MATLAB startup rules.
I wish it did not involve the graphical desktop interface, though. By looking closely in the file matlab.setting, which under Linux should lie in ~/.matlab/<your_release>/, you can find the lines
<key name="UserPath">
<string>
<value><![CDATA[<some_directory>]]></value>
</string>
</key>
where you can certainly specify manually the startup directory.
For me (I'm using Linux) it worked to put the "startup.m" to the home directory. Apparently, the home directory is by default on the matlab path. Strangely, it also worked when I put the "startup.m" into a newly created "matlab" (must be all lower-case) folder in the home directory. Let me know whether it also works on your system.