matlab - 在matlab中一次导入多个文件
问题描述
非常感谢您阅读本文;
问题:我有一个关于在 Matlab 中导入的问题。我正在尝试设计一个风力涡轮机。为此,我需要阅读许多不同的文本文件,其中包含定义翼型形状的 x 和 y 坐标(所有文本文件都放在一个文件夹中)并依次分析每个文件。
我的第一次尝试是
%% Reading the datafile
%Opening the filename in the format given to us by UIUC
fid=fopen('Filename',r);
%Skipping over the first lines (asterisk)
fscanf(fid,'%*s %*s %*s \n %*s %*s ',[1,5]);
%Reading over the Values of the columns
data=fscanf(fid,'%f %f \n',[2, inf]);
%Extracting the different types of data from the datafile
x=data(1,:);
y=data(2,:);
这样的代码有两个问题:
第一个问题是我需要在第一列中删除可变数量的单词
问题 1:有没有办法只从数据库中删除第一列数据,或者我必须删除所有值?
第二个问题是我有 1550 个表要查看并且更改文件名 1550 次将非常繁琐且耗时。
问题2:有没有办法一次打开文件夹中的所有文件?
解决方案
读取表格文件的一种简单方法是使用readtable
. 例如,如果您知道您的文件有 2 个要跳过的标题行,那么您可以简单地运行
t = readtable('my_file', 'HeaderLines', 2);
但是,这确实意味着您使用的是表而不是 MATLAB 数组。如果您更喜欢保留一个数组,您可以简单地传递表格table2array
(下面的示例)。
在处理大量文件方面,假设您的工作目录有以下文件:
a1.txt a3.txt b1.txt ...
a2.txt a4.txt b2.txt
您应该首先获得一个包含所有文件名的数组,然后遍历该数组。一种方法如下。
all_files = dir('*.txt') % Use a suitable glob to match your filenames
for i = 1:length(all_files)
data = table2array(readtable(all_files(i).name, 'HeaderLines', 2);
% do stuff
end
或者,如果您想手动读取文件:
all_files = dir('*.txt') % Use a suitable glob to match your filenames
for i = 1:length(all_files)
fid = fopen(all_files(i).name, 'r');
% do stuff
fclose(fid);
end
请注意,代码假定所有数据文件具有相同的格式。
推荐阅读
- java - 提出适当的正则表达式的问题
- python - 如何在 EC2 上制作一个正在运行的烧瓶 - 允许从外部访问?
- sqlcipher - 如何提高 sqlicifer 的性能?
- azure - 使用 U-SQL 查询 JSON 嵌套对象
- javascript - Google Charts - 通过 PHP echo 放置数据
- javascript - 动态 HTML 元素创建
- php - 可以格式化没有日期的日期
- python - 查找条件选择Python numpy的索引
- gcc - 与指定版本的 GLIBC 库链接
- tcp - 来自 TCP 客户端的奇怪行为(FIN ACK 后跟来自客户端的 SYN 请求)