Read .txt file with dots in filename

4 ビュー (過去 30 日間)
Florian
Florian 2025 年 4 月 17 日
コメント済み: Florian 2025 年 4 月 17 日
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 件のコメント
Mathieu NOE
Mathieu NOE 2025 年 4 月 17 日
maybe there is another issue (not the filename itself) - so could you share your code and one file ?
Florian
Florian 2025 年 4 月 17 日
Hi,
thanks there was indeed an issue with the testfile and I got it solved now!

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2025 年 4 月 17 日
編集済み: Stephen23 2025 年 4 月 17 日
"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 件のコメント
Florian
Florian 2025 年 4 月 17 日
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!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 4 月 17 日
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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by