matlab global documents folder path - matlab

In my matlab script I would like to output some files in a specific folder.
My function will accept a path to the desired folder and will check for its existance, but I would also like to implement a default behaviour consisting in saving everything in a default folder (named with day and time) located in the document folder of the computer.
Looking around I found some suggestion (get 'Documents' path in Matlab) on how to detect the correct folder in specific conditions (let's say a Windows PC, the default document folder, something with a specific name, ...). Is there anything more general, usable for all platforms (Win, Mac and Unix)? Or should I implement some switch?
Thanks.

Just making an answer out of Wolfie's comment.
Directly taken from Matlab documentation:
userpath('reset') sets the first folder on the search path to the
default for your platform. The default userpath folder is
platform-specific.
Windows® platforms — %USERPROFILE%/Documents/MATLAB.
Mac platforms — $home/Documents/MATLAB.
Linux® platforms — $home/Documents/MATLAB if $home/Documents exists.

Related

How to automatically delete Dymolas build files after simulation?

Every time I simulate in Dymola, a number of "useless" (for me) files are created in the working directory - i.e. dsfinal.txt, dsin.txt, dslog.txt, dsmodel.c, dymosim.exe. I find it annoying as it messes up my directory.
Is there a way to select only the desired output files to be kept after the simulations, without the need of manually deleting the undesired ones?
Those are temporary, but necessary files for Dymola. As far as I know there is no option to delete them automatically. Of course you could script that, but I don't see a real point to it and those files are used by some functionality - e.g. dsfinal.txt is used when as simulation is continued.
Some notes: Those files are created in the working directory - which should contain temporary files only. The working directory can be set via the GUI using File -> Options -> Settings:
A rather common problem is, that there is a Open and a Load function in Dymola:
As the description states, Load does not influence the working directory, whereas Open sets it to the directory from which a file is opened. The latter is also true for opening files e.g. via a double-click from the explorer. So usually it is better to go with Load.
My advice would be to separate the directories in which models/packages are stored and the working directory. This way the working directories content can be fully deleted basically anytime...

FileMaker best practice regarding user data directories etc.?

I'm working on a FileMaker 18 solution. One of the things I have to solve is executing an external Python script and reading its results back. That script can run for some seconds. Since my solution should work cross-platform (I develop on MacOS, but most users will be on Windows), I settled on the Basic Elements plugin to do the script execution.
Since this is a custom script that we'll deliver together with the solution, FileMaker needs to know in which directory it is located (and also to import the resulting XML). To my surprise, BE_ExecuteSystemCommand defaults to the root directory, not to the directory the solution is installed in.
What is the best practice to get and store a data or other directory that could be user and/or system specific?
My current thought was to create a simple "globals" key/value pair table to store paths and such, and write a script that on launch checks if a value with a key of "datapath" is set, if yes, store it in a global variable other scripts can reference. If not, prompt the user with a file open dialog to select that directory. He would only have to do that once, when launching the solution the first time.
Before doing all that myself, I'm asking here if there is a best practice on how to do that, specifically, or if I'm thinking wrong and should be doing it in some other way?
I will likely be needing the data directory for a few other purposes (data import, etc.) as well.
Filemaker has native functions that return some common paths:
Get(FileMakerPath) - path to the FileMaker application running the current file;
Get(FilePath) - path to the current file;
Get(DocumentsPath) - path to the Documents folder of the current user;
Get(DesktopPath) - path to the desktop folder of the current user;
Get(PreferencesPath) - path to the preferences folder of the current user.
Get(TemporaryPath) - path to the temporary folder of the current user;
Note that the paths returned by these functions are Filemaker paths, not OS paths.

setting a default matlab path at startup

My team is trying to standardise our Matlab paths so that everyone has the same.
I have a list of the default matlab path that we should all have.
So we would like to have a script that runs when matlab opens to make sure that our paths are set to the default matlab path. So if a path has been added to our default list it will be added in the correct place.
Is this possible in Matlab?
I read about startup but that seems to do with set your working directory which is different to what I am trying to do.
You can change which directory MATLAB starts in using the userpath function so that whenever you start up MATLAB, the path will automatically redirect here.
This may be useful if you have MATLAB running on a network per se, and multiple instances can start in the same network directory.
See more from MathWorks here: http://www.mathworks.com/help/matlab/matlab_env/matlab-startup-folder.html
However, if you want to standardize everything so that everyone has access to the same path, you can use startup to add directories / folders to MATLAB's path, but if you want to complete the package, use userpath to get MATLAB to start at a specified directory.
Your startup.m file may look something like this:
addpath('/folder/to/add/one');
addpath('/folder/to/add/two');
addpath('/folder/to/add/three');
addpath('/folder/to/add/four');
Then set your userpath with the function to complete everything:
userpath('/folder/to/start');
addpath('/folder/to/start');
Also make sure you add this new folder to your startup.m file too.
Include a path or addpath line in file startup.m. For example, to add folder aaa\bbb to the path the line would be
addpath('aaa\bbb')
Note that each user may have a different startup.m file. You may need to create it, if it doesn't already exist.

matlab creating paths to stop copying code

I have created a few general function in MATLAB that I intend to use for a few separate projects. However I do not want to copy the function into each separate project function.
I have created a folder called Misc_Function when I have placed these general functions. I know I can reference this functions explicitly by using the path and function name when trying to call the functions.
I believe you can add a path (in my case 'H:\MyTeam\Matlab\Misc_Function') when MATLAB loads up is that correct and if so how do you do this?
Assuming the above can be done I'm interested to know how MATLAB finds the correct function. In my understanding (guess work) MATLAB has a list of paths that it check trying to find a function with the name specified - is that correct? If so what happens when there are functions with the same name?
MATLAB indeed has its own search path which is a collection of folders that MATLAB will search when you reference a function or class (and a few other things). To see the search path, type path at the MATLAB prompt. From the documentation:
The order of folders on the search path is important. When files with the same name appear in multiple folders on the search path, MATLAB uses the one found in the folder nearest to the top of the search path.
If you have a set of utility functions that you want to make available to your projects, add the folder to the top of the search path with the addpath function, like so
addpath('H:\MyTeam\Matlab\Misc_Function');
You have to do this everytime you start MATLAB. Alternatively, and more conveniently, save the current search path with the savepath command or add the above commands to your startup.m file.
You can check the actual paths where Matlab searches for functions using
path
You will notice, that the most top path (on start up) is a path in your home folder. For Linux this is e.g. /home/$USER/Documents/MATLAB. For Windows it is somewhere in the the c:\Users\%USER%\Documents\Matlab (I think). Placing a file startup.m in this folder allows to add additional paths using
addpath('H:\MyTeam\Matlab\Misc_Function');
or
addpath(genpath('H:\MyTeam\Matlab\Misc_Function'));
on start up of Matlab. The latter (genpath) allows to also add all subdirectories. Simply write a file startup.m and add one of above lines there.
I believe 'addpath' will add the folder to MATLAB path only for the current MATLAB session. To save the updated path for other sessions, you need to execute 'savepath' command.
As mentioned in the previous comments, adding the folder in startup.m is a good idea since it will be added to the path on MATLAB startup.
To answer your question about how MATLAB finds the correct function, MATLAB maintains a list of directories in its path in a file called pathdef.m. Any changes to the path will be written to this file (when you execute 'savepath'). The path variable is initialized with the contents of this file.

temporary remove all of the added paths in matlab

I have added a lot of paths to matlab and now I want to share my code, but I don't know exactly which function (in which path) I should add to my shared code. Each time I need to add a missing function and it is really bothering for me and the users who are using the code.
So, I would like to restore the matlab path to its original case. Is there any way to do this in matlab? I also want to keep a backup of my current added path in a .m file and use it later when I am done.
To restore the path to default value - http://www.mathworks.com/help/matlab/ref/restoredefaultpath.html
restoredefaultpath sets the search path to include only folders for
MathWorks® installed products. Use restoredefaultpath when you are
having problems with the search path.
restoredefaultpath; matlabrc sets the search path to include only
folders for MathWorks installed products and corrects search path
problems encountered during startup.
And to save the current path - http://www.mathworks.com/help/matlab/ref/savepath.html
savepath updates the MATLAB® search path for all users on the system
so that the path can be reused in a future session. savepath saves the
search path to the pathdef.m file that MATLAB located at startup, or
to the current folder if a pathdef.m file exists there.
Or you can just store path in variable p = path; and restore it later path(p);. If the path is saved into pathdef.m the call of pathdef returns the string that can be used to set the saved path.