Loading files in Matlab - matlab

In my code I want to indicate paths to files that are independent on which computer the code is run. In other words, I want the script to operate only in the folder where it is located because my friend's computer can have different drive names, folders' names etc. The task is to load files by indicating where they are located. I thought this would work: "load("..\Folder\file.mat")". However, Matlab gives an error
Error using load
'..\Folder\file.mat' is not found in the current folder or on the MATLAB path, but exists in:
D:\D (synced)\Folder 3\Matlab
D:\D (synced)\Folder 3\Data
Change the MATLAB current folder or add its folder to the MATLAB path.
I checked and file.mat is in Folder which is located in the same directory as the script that I run.
Could someone tell how to make all paths independent on what computer they are run and avoid the error?

I suppose that both your script and file are in the folder Folder.
To make it operating system independent, you could use mfilename to retrieve the path of your script, and use fullfile to concatenate the path with the file name.
p = mfilename( 'fullpath');
file = load( fullfile( p, 'file.mat'));

Related

Adding a Folder path in Matlab to access a different folder with Matlab data on Mac

I am trying to load a folder path on mac to a folder of MATLAB data sets.
This is the code I am using:
folder_path = '/Documents/2020/Root Locus/Data';
files = dir(fullfile(folder_path,'*.mat'))
The file I am running the code from is located in the root locus folder. My files variable keeps appearing empty as nothing has loaded to it, any help?
You need to write the path as either the relative path from Matlab's current folder or the absolute path for the system. An absolute path will start with / and specify the location from the topmost system directory. Relative paths do not start with /.
If you are running Matlab from Root Locus, then try folder_path = 'Data';
The absolute path makes the code more portable, so also try folder_path = '/Users/<username>/Documents/2020/Root Locus/Data';

How to install q in another directory?

I am trying to install q in a directory other than my home directory. Is this possible? It seems q will fail if it is in another directory. I get this error when trying to run it:
'/Users/cammil/q/q.k. OS reports: No such file or directory
0::
`/Users/cammil/q/q.k
Set your QHOME to directory which contains q.k file. That should solve the issue.
Also, if that directory does not contain k4.lic (license) file then set QLIC to directory containing license file.
Read more details about environment variables here: https://code.kx.com/q4m3/14_Introduction_to_Kdb+/#1481-the-environment-variables
KDB+ uses the QHOME environment variable at startup. QHOME specifies where to find the q.k file, and if it is not defined kdb will by default look into the home directory. It will also look for the licence file in the same way. Therefore you must define your QHOME variable as the directory which holds the .q.k and k4.lic files. (Or alternativley you can define the QLIC variable for the license)

To delete a file, located in the search path, in MATLAB

I have a file located in /path/to/Matlab/myData.mat. The path /path/to/Matlab has been in the search path. I launch Matlab from /path/to/Matlab/workspace/.
However, the command if (exist('myData.mat','file')==2) delete('myData.mat'); returns error saying that File myData.mat cannot be found.
There is only one file named myData.mat among all the search paths. Is it mandatory to use absolute or relative path when call the delete() function providing the location has been added to the search path?
My OS is Ubuntu 16, and Matlab v2015b.
Use which to find the full path

Accessing folders via MATLAB

I am trying to access some .m folders that I have downloaded into Downloads. How can I access this folder and run the files using cd similar to how I would on a Terminal (MacOS)? The same statements don't work.
All of the regular commands for linux should work.
ls, dir, cd 'your folder goes here' , cd ,, , cd.. etc
You need to pass the absolute path of your Downloads folder, e.g.:
current_dir = pwd;
cd('C:\Users\you_username\Downloads')
Then, you'll be able to access the files in that folder. When you are finished, you can then return to your original folder with:
cd(current_dir);
An alternative is to add the Downloads folder to your MATLAB path with addpath. You should then be able to access files in there from any directory of your choosing without having to cd. If you want to make that change to the MATLAB path permanent, use savepath afterwards to save the MATLAB path for future sessions.

MATLAB path doesn't match pathdef

I am running MATLAB 2013b on a CentOS machine. Right now I have startup.m set to cd me into another directory, lets call it shared, where I keep all of my code. I also have pathdef.m set to add shared and some of its subdirectories to the MATLAB path.
The problem is that once MATLAB is open and I check the path settings, ~/matlab has also been added to the top of the path list, ahead of shared. The home folder is where I keep some old versions of code, so it causes the wrong version to be run sometimes. I've double checked my pathdef and startup files, and the ~/matlab directory is definitely not listed. What could be causing MATLAB to automatically add this directory to the path, and how can I fix it?