Read .txt file with dots in filename
4 ビュー (過去 30 日間)
古いコメントを表示
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
2025 年 4 月 17 日
maybe there is another issue (not the filename itself) - so could you share your code and one file ?
採用された回答
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)
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.
その他の回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!