在Matlab中使用feof函数制作从文件中读取的字符串矩阵 [英] Making a matrix of strings read in from file using feof function in matlab

查看:544
本文介绍了在Matlab中使用feof函数制作从文件中读取的字符串矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个索引文件(称为runnumber_odour.txt),如下所示:

I have an index file (called runnumber_odour.txt) that looks like this:

run00001.txt   ptol
run00002.txt   cdeg
run00003.txt   adef
run00004.txt   adfg

我需要某种方式将其加载到matlab中的矩阵中,这样我就可以搜索第二列以找到那些字符串之一,加载相应的文件并对其进行一些数据分析. (即,如果我搜索"ptol",它将加载run00001.txt并分析该文件中的数据.)

I need some way of loading this in to a matrix in matlab, such that I can search through the second column to find one of those strings, load the corresponding file and do some data analysis with it. (i.e. if I search for "ptol", it should load run00001.txt and analyse the data in that file).

我已经尝试过:

I've tried this:

clear; clc ;
% load index file - runnumber_odour.txt
runnumber_odour = fopen('Runnumber_odour.txt','r');
count = 1;
lines2skip = 0;

while ~feof(runnumber_odour)

 runnumber_odourmat = zeros(817,2);
 if count <= lines2skip
     count = count+1;
     [~] = fgets(runnumber_odour); % throw away unwanted line
     continue;
 else
     line = strcat(fgets(runnumber_odour));
     runnumber_odourmat = [runnumber_odourmat ;cell2mat(textscan(line, '%f')).'];
     count = count +1;
   end
end

runnumber_odourmat

但这只会产生一个零乘2的817矩阵(即不写入矩阵),但是没有运行号runnumber_odourmat = zeros(817,2);我收到错误消息未定义的函数或变量'runnumber_odourmat'.

But that just produces a 817 by 2 matrix of zeros (i.e. not writing to the matrix), but without the line runnumber_odourmat = zeros(817,2); I get the error "undefined function or variable 'runnumber_odourmat'.

我也用strtrim而不是strcat尝试过,但这同样行不通,有同样的问题.

I have also tried this with strtrim instead of strcat but that also doesn't work, with the same problem.

那么,如何将文件加载到matlab中的矩阵中?

So, how do I load that file in to a matrix in matlab?

推荐答案

您可以使用Map对象轻松完成所有这些操作,因此您无需进行任何搜索或类似操作.您的第二列将是第一列的键.代码如下

You can do all of this pretty easily using a Map object so you will not have to do any searching or anything like that. Your second column will be a key to the first column. The code will be as follows

clc; close all; clear all;
fid = fopen('fileList.txt','r'); %# open file for reading
count = 1;
content = {};
lines2skip = 0;
fileMap = containers.Map();
while ~feof(fid)
    if count <= lines2skip
        count = count+1;
        [~] = fgets(fid); % throw away unwanted line
    else
        line = strtrim(fgets(fid));
        parts = regexp(line,' ','split');
        if numel(parts) >= 2
            fileMap(parts{2}) = parts{1};
        end
        count = count +1;
    end
end
fclose(fid);

fileName = fileMap('ptol')

% do what you need to do with this filename

这将提供对任何元素的快速访问

This will provide for quick access to any element

然后,按照我提供的答案,您可以执行上一个问题中所描述的操作.

You can then do what was described in the previous question you had asked, with the answer I provided.

这篇关于在Matlab中使用feof函数制作从文件中读取的字符串矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆