Where do MATLAB-created .txt files get saved?

30 views (last 30 days)
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!

Accepted Answer

Jan
Jan on 10 Nov 2021
Edited: Jan on 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.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!