Does Matlab have "project files"? - matlab

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.

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:

How do I write handles from a MATLAB GUI into a .m file?

I am trying to create a GUI for a script that receives paths and matrix names from the user (via uigetdir and uigetfile functions as well as edit text fields), and finally upon pushing the "Run" button writes these handles into a .m file and runs another script.
Essentially, in the end it should create a file called 'RunName'.m that looks something like:
base_path = get(handles.BasePathEdit,'String')
file_name = get(handles.FileNameEdit,'String')
main % runs the main script with the preceding variables defined as they were
and run it.
I'm guessing the script would run successfully if I just write it like that under the 'Run' push-button function, but I would still like for it to create a .m file as described.
Thank you very much for you help,
All the best.
The simplest way of accomplishing this is to write a function that executes upon the pressing of the 'run' button that reads the values from the GUI and the proceeds with the main script. In guide, this is simple - right-click on the 'run' button, and under 'View Callbacks', select 'Callback'. Then you can use the usual syntax get(handles.RELEVANT_TEXT_BOX,'string') etc to read the resr of the data in. You can then pass this to your main function, or you could then just copy and pase your script straight into the callback.

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.

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.

setting GUI's workspace as the currend workspace

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.