How to load data in MATLAB from current script folder? - matlab

There is a script myScript.m, under D:\myProjects, so its location is D:\myProjects\myScript.m
I want to load a .dat file at D:\myProjects\myData\someData.dat
How do I use the load() function without using the absolute path like
data = load('D:\myProjects\myData\someData.dat') % something I do not want

data = load('myData\someData.dat')
Use a relative path. This assumes you are executing the program from it's home directory D:\myProjects.
If you need to call the script from different folders you should pass the path to the .dat as an argument to the script.

Related

Creating a new file in the script folder using fopen

I'm using MATLAB R2017a and I would like to create a new binary file within the folder the script is running from.
I am running matlab as administrator since otherwise it has no permissions to create a file. The following returns a legal fileID:
fileID = fopen('mat.bin','w');
but the file is created in c:\windows\system32.
I then tried the following to create the file within the folder I have the script in:
filePath=fullfile(mfilename('fullpath'), 'mat.bin');
fileID = fopen(filePath,'w');
but I'm getting an invalid fileId (equals to -1).
the variable filePath is equal in runtime to
'D:\Dropbox\Studies\CurrentSemester\ImageProcessing\Matlab
Exercies\Chapter1\Ex4\mat.bin'
which seems valid to me.
I'd appreciate help figuring out what to do
The problem is that mfilename returns the path including the file name (without the extension). From the documentation,
p = mfilename('fullpath') returns the full path and name of the file in which the call occurs, not including the filename extension.
To keep the path to the folder only, use fileparts, whose first output is precisely that. So, in your code, you should use
filePath = fullfile(fileparts(mfilename('fullpath')), 'mat.bin');

Matlab load file in path of script

I have a matlab script that wants to load a .mat file that is in a directory fixed relative to the location of the script. The script itself could be in different places relative to the current working directory, so the location of the .mat file is not known relative to it. How do I specify the location of the file to load relative to the script that is executing?
The function mfilename returns the name of the currently running script. This however does not return the full path to the script. You probably want this and so you can specify the 'fullpath' option to return the full path to the actual script itself, including the name of the script.
You just want the actual directory of where the file is, and so first use mfilename to get the full path to the actual file, then use fileparts to actually extract the actual directory of where the file is. fileparts returns the directory of where the file is, the file name itself and the extension. You just want the first output argument and don't care about the other outputs. Once you have this, you can then use the actual directory then append this string with the location of your .mat file:
p = mfilename('fullpath');
[pathstr,~,~] = fileparts(p);
d = fullfile(pathstr, 'path', 'to', 'your', 'file.mat');
fullfile builds a directory string that is OS independent, so for each subdirectory you want to indicate to get to your .mat file, place these as separate input strings up until you reach the file you want. d will contain the full path of your .mat file relative to the currently running script, which you can then use to load accordingly.

How to get relative path to folder containing a .m file

I write code where I load a lot of project data. I want to keep my pathnames in the code relative to some location of this project on the disk, i.e. not having it configured or hard-coded.
Is there function in matlab to do some like this?
In python I would write:
ROOT = os.path.dirname(__file__)
The best way to do this is to combine fileparts with mfilename('fullpath'). (All examples assume the executing m-file containing these statements lives in /home/suever/code/stackoverflow.m)
mfiledir = fileparts(mfilename('fullpath'));
/home/suever/code
Then you can use fullfile to construct any paths you need from that. Now if you have a file (data.mat) stored in the same directory:
filename = fullfile(mfiledir, 'data.mat');
/home/suever/code/data.mat
Of if the file is actually in the parent directory.
filename = fullfile(mfiledir, '..', 'data.mat');
/home/suever/data.mat
If you want just the parent directory that an m-file is in, you can apply fileparts twice and only keep the second output.
[~, reldir] = fileparts(fileparts(mfilename('fullpath')));
code
I would recommend the use of full paths in the first examples as those are completely independent of the user's current working directory.
A better recipe to organize your code is to have a function like this:
function [ path ] = get_path()
path = [regexprep(mfilename('fullpath'), ['\' filesep '[\w\.]*$'],'') filesep];
end
You drop it inside +foo/get_path.m file and than call something like foo.get_path() that returns the path to +foo folder.

process a list with specific name and extension in matlab

I'm trying to process a list of files that start with the same string, but only the .mat files. In my folder I have log files with names such as:
CADS3P5Ph1_LKS_20141210_EVAL_103443_001.avi
CADS3P5Ph1_LKS_20141210_EVAL_103443_001_MeasData.mat
CADS3P5Ph1_LKS_20141210_EVAL_103443_002.avi
CADS3P5Ph1_LKS_20141210_EVAL_103443_002_MeasData.mat
CADS3P5Ph1_LKS_20141210_EVAL_103443_003.avi
CADS3P5Ph1_LKS_20141210_EVAL_103443_003_MeasData.mat
CADS3P5Ph1_LKS_20141210_EVAL_104236_001.avi
CADS3P5Ph1_LKS_20141210_EVAL_104236_001_MeasData.mat
I only need to process the files that have the same timestamp (e.g. 103443_xxx)
I made a variable looking with a wildcard
filename = CADS3P5Ph1_LKS_20141210_EVAL_103443_001_MeasData.mat
general_name = filename(1:end - 17);
general_name = strcat(general_name,'*','');
So when I do dir(general_name), it finds all the files that start with "CADS3P5Ph1_LKS_20141210_EVAL_103443",
How do I only get the .mat files, and not the .avi files
I tried
dir(general_name && *.mat)
Is there a way to make something like this work?
Thanks!
Using strcat with general_name and the wildcard character for .mat extensions should work:
dir(strcat(general_name,'*.mat'))

how to use save wav files to current subdirectory in matlab?

My current directory is under C, for example, "C:\xxx\"
Now, I want to export my processed wav files to a subfolder in my current directory, for example, "\wav_results\".
What I have done is declaring a filepath variable:
wav_dir = '\wav_results\';
wavwrite(...., [wav_dir wav_name]) %wav_name is the name of the wav file
The error says no such file or directory. I do not want to use the full directory path for wav_dir because I need to move this script from place to place. Anyone has good suggestion?
Thanks~
Use mkdir before you call wavwrite:
wav_dir = '\wav_results\'; %'
if not(exist('testresults','dir'))
mkdir(wav_dir);
end
wavwrite(...., [wav_dir wav_name])