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.
Related
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'));
Whenever I download repositories from my team from other departments they have scripts ( .m files )in MATLAB which are runned from a path file which contain ".." ( like a shortcut ) in their path link and I do not know how to change that on my MATLAB workstation, the parent directory for it. For example an .m file (script) which contains:
MODEL_CONFIG='..\03_config\config.m';
run(MODEL_CONFIG)
On their workstation this code works but on my workstation it says that:
"there is no ..\03_config\config.m not found."
and I know that the ".." is the parent directory from the project. My question is:
"How can I change the default parent directory so that ".." can work on my workstation too?
Right now the only solution is to manually change in every script file the ".." with 'C:\Users%user%\Desktop\19_projectsMatlab\99_GSM_OEM' - and it this example 99_GSM_OEM would be the parent directory.
What a .. means in a path is basically: go back one folder from your current working directory in Matlab. You can easily change this folder by clicking on in Matlab.
If you want to change this folder during script execution you can do this with
cd 'C:/Users/yourname/yourfolder/'
Actually I am stupid. ".." is like "cd.." in MS-DOS....
I was in the wrong folder all the time. I am not supposed to be in the parent directory of the project when I run the main script. I am supposed to be in the folder directory where the main script is running (main.m).
So when I am in the folder directory of the main.m file the following link:
MODEL_CONFIG='..\03_config\config.m';
says go back with one folder from where the main.m file is and there should be the folder 03_config which you access. Thank you guys.
And if you have more subfolders in the folder in which is the main.m script "." - means the current location....
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';
I have a compiled ahk script in which I use %_A_WorkingDir% to get the current working directory. But now I'm calling the .exe from another file two folders up so %_A_WorkingDir% returns the directory that is two folders up, not the location of the actual .exe. How do I fix this issue?
A_WorkingDir is the directory in which the script is currently
working. You can change it by using
the command SetWorkingDir.
A_ScriptDir is the full path of the directory where the script is in.
A_ScriptFullPath is the full path of the current script.
https://autohotkey.com/docs/Variables.htm#prop
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.