How can I fopen files in different directory

16 vues (au cours des 30 derniers jours)
Vinoth S
Vinoth S le 25 Jan 2020
Commenté : Vinoth S le 25 Jan 2020
How can I fopen files that are in a different directory than current directory, fopen always returns -1.

Réponse acceptée

Stephen23
Stephen23 le 25 Jan 2020
Do NOT follow advice of just adding more directories to the Search Path: this just pointlessly slows MATLAB down (MATLAB has to keep track of all of the files in all folders on the Search Path). In general you should NOT add data folders to the search path, because all MATLAB functions that import/export data files accept relative or absolute filenames, and this is much more efficient than bloating the Search Path. For example:
D = 'E:\mails spamnon\non spam';
S = dir(fullfile(D,'*.*'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
fid = fopen(F);
...
fclose(fid);
end
  1 commentaire
Vinoth S
Vinoth S le 25 Jan 2020
Thanks this works perfectly!

Connectez-vous pour commenter.

Plus de réponses (1)

stozaki
stozaki le 25 Jan 2020
Hello,
Please add the path containing the file you want to open to the MATLAB search path.
Regards,
stozaki
  3 commentaires
stozaki
stozaki le 25 Jan 2020
Modifié(e) : stozaki le 25 Jan 2020
Please try following command.
addpath(genpath('E:\mails spamnon\non spam'))
Vinoth S
Vinoth S le 25 Jan 2020
Thanks!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by