Matlab:如何通过指定 header 名称读取和提取矩阵?

标签 matlab matrix text-files

是否可以从文本文件中读取指定标题下的矩阵? 我有一个像这样的文本文件:

Header A (2x3):
3 6 7
5 8 8
Header B (4x4):
23 65 2 6 
4 6 7 8
33 7 8 9

所以我想要完成的是将标题名称作为参数并获取其下的矩阵。可以用Matlab实现吗?

提前致谢!!

最佳答案

此外,尝试使用此代码:

infilename = '1.txt'; % name of your file
m = memmapfile(infilename);  % load file to memory (and after close it)
instrings  = strsplit(char(m.Data.'),'\n','CollapseDelimiters',true).';

checkstr = 'Header B';
% find all string (their indices) starting with checkstr 
ind = find(strncmpi(instrings,checkstr,length(checkstr)));


data = [];
if isempty(ind)
   fprintf('\n No strings with %s',checkstr)
else
   % first string with string checkstr

    n = ind(1)+1;
    N = length(instrings);
    while n<=N % find all numerical data after string with `checkstr`
        convert = str2num(instrings{n});
        if isempty(convert), break, end % find non-numerical data

        data(end+1,1:length(convert)) = convert; % it because you can have various number of columns
        n = n+1;
    end
end

data % display load data

输出

23    65     2     6     7
 4     6     7     8     0
33     7     8     9     0

对于文件1.txt:

Header A (2x3):
3 6 7
5 8 8
Header B (4x4):
23 65 2 6 7
4 6 7 8
33 7 8 9

关于Matlab:如何通过指定 header 名称读取和提取矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39888851/

相关文章:

MATLAB:二进制矩阵的所有可能组合

java - 在 MATLAB for Mac 中启用选项键快捷键

c - DGEMM 之后最后一行中的零值

C:将文本文件中的值保存到内存字段

linux - 如何在使用 linux diff 进行比较时添加新的空行来代替已删除的行

python - 使用python从非常大的文本文件(16gb)中跳过一行的省时方法

matlab - 如何 'copy'矩阵而不在内存中创建导致内存溢出的临时矩阵?

matlab - 旋转矩阵按列计算而不是按行计算

c - 为什么我的全局分配结构没有正确更新值?

android - 如何使用 android.graphics.Matrix 的值?