Read .txt file with dots in filename
8 views (last 30 days)
Show older comments
Hi,
I have a set of files named "xx_xx_32.5_xx_6.0_xx.txt" which I would need to read in to process with Matlab. The numbers are always changing and denote parameters of the experiment. Is there any way to read the file and import the data into Matlab, ignoring all the dots before ".txt"?
All my current tries using for example "load" or "readmatrix" failed since Matlab always assumes everything after the first dot to be the file extension.
Thanks!
3 Comments
Mathieu NOE
on 17 Apr 2025
maybe there is another issue (not the filename itself) - so could you share your code and one file ?
Accepted Answer
Stephen23
on 17 Apr 2025
Edited: Stephen23
on 17 Apr 2025
"All my current tries using for example "load" or "readmatrix" failed since Matlab always assumes everything after the first dot to be the file extension."
It works correctly here, with absolutely no special options or settings required:
F = "xx_xx_32.5_xx_6.0_xx.txt";
writematrix(rand(3,5), F)
M = readmatrix(F)
You could also specify the filetype:
M = readmatrix(F, 'Filetype','delimitedtext')
I very much doubt that MATLAB "assumes everything after the first dot to be the file extension" because this would be completely inconsistent with the behavior of FILEPARTS, which is the function that MATLAB uses to identify the filename and file extension:
[~,N,X] = fileparts(F)
But without you providing any code or showing us any working examples of what you claim then we have to rely on the MATLAB documentation and functions, which as I showed here clearly do work as expected.
More Answers (1)
Walter Roberson
on 17 Apr 2025
projectdir = '.'; %directory that contains the data
dinfo = dir(fullfile(projectdir, 'xx*.txt'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfiles = numel(filenames);
data = cell(numfiles, 1);
for K = 1 : numfiles
FILENAME = filenames{K};
data{K} = readmatrix(FILENAME, 'filetype', 'text');
end
0 Comments
See Also
Categories
Find more on Environment and Settings 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!