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

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

Related

Loading files in 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'));

Get Current Path in Jaseci

I am in need of getting the current dir path of the jac file i am working on. This is beneficial for me to access files from relative location without alway moving to the file directory to run the jac file.
Python Alternative
import os
jac_dir = os.path.dirpath(__file__)
Currenly I have to assume the working directory and sey the file path or i have to always move the working directory to the jac file directory and run the jac the file.

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.

What is "." in matlab search path?

I'm trying to set up the search path of my laptop matlab. I saw "." in my office computer matlab(set up by someone else).
What does . mean?
How can I add . also as my search path. I tried to add it but it failed.
There are two methods for specifying paths: absolute and relative paths.
An absolute path is e.g. C:\some\folder in Windows, or e.g./Some/folder in Linux and Mac. As the name says, these are absolute and always the same. Relative paths however are paths specified in relation to the current working directory.
When creating relative paths, . is the current working directory. .. similarly is the parent directory. This can be used to add subfolders to the path, e.g. ./subfolder or add another directory which in located in the parent directory, e.g. ../other_directory.
Afaik, you don't need to add the current working directory to the search path, as this is the default behavior of MATLAB.