Matlab is in the wrong working path, how to set it?

Hello,
i try to load a file, that is in the folder '/functions'
I tried to add this by addpath('\function) but this didnt work. Only addpath([pwd '/functions/']) works.
the command pwd and cs gives out the right directory but if i try to load the file it says:
Error using load
Unable to find file or directory 'C:\file.mat'.
It seems that the matlab is working in an other directory. I dont know how to change it.
I reinstalled matlab allready, but the prefferences are saved somewhere.
I also tried to set the paths(home/set paht) to default, didnt work
Can I set whole matlab to default?
Thanks for your help

5 Comments

"i try to load a file, that is in the folder '/functions'"
It is not required to change the MATLAB search path to import/export data files, just use an absolute/relative filename.
"I tried to add this by addpath('\function) but this didnt work."
I don't see why it should: the ADDPATH documentation states that you must "Use the full path name for each folder", which your example clearly is not (not in Win OS nor Linux... not sure about MacOS).
"It seems that the matlab is working in an other directory. I dont know how to change it."
MATLAB "works" in the current directory. I find changing it is easiest in the browser.
Thats the issue.
I'm using realtive filenames, but it only works with absulote. Because matlab things it is in C://
I did the restore command....
still only works with absolute filenames, bc matlab does not thake the working dir as it should be
"I'm using realtive filenames..."
The error message you give shows that you are not: "Unable to find file or directory 'C:\file.mat'." clearly does not include the subfolder in the filename. Changing the MATLAB Search Path is not required (nor recommended) for accessing data files.
"Because matlab things it is in C://"
Please show the output of this command:
pwd
pwd gives out the directory the script is in.
This folder includes the subfolder /function but it is not possible to include this one by a relative path
>> pwd
ans =
'C:\Users\20220031\Downloads\OCT_Reconstruction_and_Spectral_Analysis-master'
>> ls
. ROI_analysis runSpectralV3.m
.. display_functions spectralParamsV3_2D.m
Contast enhanced OCT.pdf functions spectralParamsV3_3D.m
Contrast enhanced OCT supplementary.pdf lymph_functions xml_io_tools
LICENSE reconstructParams.m
README.md runReconstruct.m
>> addpath('\xml_io_tools\');
Warning: Name is nonexistent or not a directory: C:\xml_io_tools
> In path (line 109)
In addpath (line 86)
"pwd gives out the directory the script is in. "
No, PWD gives the current directory. Whether the script is in the current directory or not is irrelevant to PWD.
"This folder includes the subfolder /function.."
There is no directory named FUNCTION in the LS output you show. But there is one named FUNCTIONS.
"but it is not possible to include this one by a relative path"
Why are you still attempting to use relative paths with ADDPATH?:
addpath('\xml_io_tools\');
As I already explained here, ADDPATH accepts only absolute filenames.

Sign in to comment.

Answers (1)

Generate full (or relative) path to the file you want to load:
data_dir = '/home/me/data';
data_file = 'mydata.mat';
file2load = fullfile(data_dir,data_file);
Load said file:
load(file2load)
Use the matlab-path for its intended purpose: expand the search-tree of toolboxes with matlab-functions.
HTH

Categories

Products

Release

R2022b

Asked:

on 4 Oct 2022

Edited:

on 5 Oct 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!