Where do MATLAB-created .txt files get saved?

20 vues (au cours des 30 derniers jours)
Alexander Ketzle
Alexander Ketzle le 10 Nov 2021
I'm a student looking to do some basic data analysis from files using MATLAB, and was learning how to use fopen and related commands, currently I'm making a dummy file and reading it using this code:
fileName = 'nums.txt';
x = 1:1:5;
y = [x;rand(1,5)];
fileID = fopen(fileName,'w');
fprintf(fileID,'%g %g\n',y);
fclose(fileID);
disp('file start');
type nums.txt
disp('file end');
fileID = fopen(fileName,'r');
formatSpec = '%g %g';
sizeA = size(y);
A = fscanf(fileID,formatSpec,sizeA)';
I just have one question: Where does MATLAB store this file? Is there a specified directory? And could I specify the full path of where the file gets saved?
Thank you!

Réponse acceptée

Jan
Jan le 10 Nov 2021
Modifié(e) : Jan le 10 Nov 2021
If you do not specify a folder, the current directory is used. See
help cd
help pwd
Note, that pwd is just a wrapper function for cd , so prefer the later command.
Of course you can specify a folder:
fileName = fullfile(cd, 'nums.txt');
fileName = fullfile(tempdir, 'nums.txt');
fileName = 'D:\my\sub\folder\nums.txt'
... etc
After starting Matlab the current folder can be defined differently according to the chosen preferences. It can be a fixed folder, the last active folder in the former Matlab session or the user folder, e.g. under Windows "%APPDATA%\MATLAB" or similar.
  1 commentaire
Alexander Ketzle
Alexander Ketzle le 10 Nov 2021
Super helpful and informative, thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings 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