Read .txt file with dots in filename

30 views (last 30 days)
Florian
Florian on 17 Apr 2025 at 7:27
Commented: Florian on 17 Apr 2025 at 7:56
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
Mathieu NOE on 17 Apr 2025 at 7:52
maybe there is another issue (not the filename itself) - so could you share your code and one file ?
Florian
Florian on 17 Apr 2025 at 7:56
Hi,
thanks there was indeed an issue with the testfile and I got it solved now!

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 17 Apr 2025 at 7:37
Edited: Stephen23 on 17 Apr 2025 at 7:40
"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)
M = 3×5
0.6715 0.4446 0.2202 0.6635 0.4359 0.1113 0.4803 0.3495 0.8802 0.2290 0.3527 0.4785 0.8508 0.2626 0.8446
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You could also specify the filetype:
M = readmatrix(F, 'Filetype','delimitedtext')
M = 3×5
0.6715 0.4446 0.2202 0.6635 0.4359 0.1113 0.4803 0.3495 0.8802 0.2290 0.3527 0.4785 0.8508 0.2626 0.8446
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
N = "xx_xx_32.5_xx_6.0_xx"
X = ".txt"
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.
  1 Comment
Florian
Florian on 17 Apr 2025 at 7:50
Hi,
thanks for your detailed answer. I don't know how it happened or when but apparently the first file to read in got corrupted during my tries and I did not notice until now - I got it to work with the others now as well and understand Matlabs file read-in much better now!
Thanks!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Apr 2025 at 7:41
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

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!