How to load data from other directory?

1.203 visualizaciones (últimos 30 días)
Mr M.
Mr M. el 26 de Nov. de 2015
Comentada: Moon el 3 de Mzo. de 2024
I want to load data from other director, but I want to set relative path instead of absolute. So how to step back from current directory? /..

Respuestas (5)

Stephen23
Stephen23 el 26 de Nov. de 2015
Editada: Walter Roberson el 5 de Feb. de 2021
This is explained quite well in the documentation:
Under the title "Absolute and Relative Path Names" there is the following information:
Some MATLAB functions also support relative path names. Unless otherwise noted, the path name is relative to the current folder. For example:
  • myfile.m refers to the myfile.m file in the current folder.
  • ./myfolder refers to the myfolder folder in the current folder.
  • ../myfolder/myfile.m refers to the myfile.m file in the myfolder folder, where myfolder is at same level as the current folder. Each repetition of ../ at the beginning of the path moves up an additional folder level.
  2 comentarios
Pen-Li (Ben) Yu
Pen-Li (Ben) Yu el 5 de Feb. de 2021
To refer to the myfolder folder in the current folder, a forward slash shall not be employed:
  • myfolder refers to the myfolder folder in the current folder.
Walter Roberson
Walter Roberson el 5 de Feb. de 2021
Stephen got tripped up by conversion to bullet form.
Using ./myfolder (with leading period) is a valid way to refer to myfolder within the current folder. It has some advantages in that using ./myfolder tells MATLAB not to search for myfolder along the MATLAB path. For example
exist('./myfolder', 'dir') %is there one HERE ??
exist('myfolder', 'dir') %is there one somewhere on the path ?

Iniciar sesión para comentar.


sarwat firoz
sarwat firoz el 14 de Dic. de 2020
Case 1: If your file is in the same folder, as the file you are using to load the data.
load fileName;
Case 2: If your file is in a folder INSIDE your directory,
load folderName/fileName;
Case 3: If your file is one step OUTSIDE your directory,
load ../fileName;
Case 4: If your file is n step OUTSIDE your directory,
load ../../../../ ... ... ... ../../fileName;
% Please make as many as ../ as much as up you want to go in folder structure.
% Kindly ignore ellipsis.
Case 5: If you wanna go up 2 folders and from there you wanna enter a separate folder structure,
load ../../folderStructure/fileName;
% Please use it the way you want.
P.S. Windows users can use either backward slash or Linux.
References:
  1. GNU Octave Free your Numbers
  2. GNU Octave (version 6.1.0)
  1 comentario
Moon
Moon el 3 de Mzo. de 2024
for case 5, how do I specify folder structure

Iniciar sesión para comentar.


Titus Edelhofer
Titus Edelhofer el 26 de Nov. de 2015
Editada: Titus Edelhofer el 26 de Nov. de 2015
Hi,
you simply need to remove the / at the beginning:
dir ..\temp
dir ..\..\anotherFolder\anotherSubfolder
Note: replace "\" of course with "/" on Linux or OSX
Or use the generic functions:
% go to folders up the hierarchy:
upUpFolder = fileparts(fileparts(pwd));
% go into another folder
folder = fullfile(upUpFolder, 'subFolder');
% do whatever you like
dir(folder)
Titus

Thorsten
Thorsten el 26 de Nov. de 2015
Editada: Thorsten el 26 de Nov. de 2015
Use '..' to refer to the folder that contains the current folder. To load ../folderName1/folderName1/filename.ext robustly on different OSs, use:
load(fullfile('..', 'folderName1', 'folderName2', 'filename.ext'))
you can add as much folderNames as you need.
  1 comentario
Titus Edelhofer
Titus Edelhofer el 27 de Nov. de 2015
Jepp, that's easier than using fileparts ...

Iniciar sesión para comentar.


Suhas gowda
Suhas gowda el 6 de Mayo de 2023
This works well for me
[file,path] = uigetfile('*.mat'); % opens file selection dialog box. You may choose data from different folder
A=importdata(strcat(path,file)); % data will be imported from the specified path
  1 comentario
Walter Roberson
Walter Roberson el 6 de Mayo de 2023
The path returned by uigetfile does not reliably end in a directory separator. Using strcat(path, filename) will sometimes fail. We recommend that you use fullfile(path, filename) which will ensure that correct separators are used.

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by