Matlab的阅读txt文件到数组 [英] Matlab read in txt file into array

查看:810
本文介绍了Matlab的阅读txt文件到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的信息格式的文本文件:

I have a text file with the following format of information:

Name1 34 25 36 46
Name1 23 53 15 86
Name1 25 25 87 35
Name2 76 22 44 55
Name2 88 88 88 88
Name3 11 11 11 11
Name3 55 66 88 88
Name3 88 88 88 88
Name3 00 00 00 00

有不同的名字,我必须每名安排到一个数组插槽。那么我就需要另一种方式来分配,每行到特定的位置相关的日期。因此,例如,第一名1可具有阵列{0},但我还需要在34,24,36,和46以某种方式相关联。我还需要区分彼此不同的名称。什么是做到这一点的最好方法是什么?一个2x2的阵列似乎并没有成为解决方案。

There are different "Names" and I have to arrange each name into an array slot. I would then need another way to allocate the date associated with each row to that specific spot. So for example, the first Name1 may have array{0}, but I would also need to associate the 34, 24, 36, and 46 somehow. I would also need to distinguish the different names from each other. What is the best way to do this? a 2x2 array does not seem to be the solution.

我至今是沿着这个路线的东西:

What I have so far is something along the lines of this:

%# read the whole file to a temporary cell array
fid = fopen(filename,'rt');
tmp = textscan(fid,'%s','Delimiter','\n');
fclose(fid);

%# remove the lines starting with headerline
tmp = tmp{1};
idx = cellfun(@(x) strcmp(x(1:10),'headerline'), tmp);
tmp(idx) = [];

%# split and concatenate the rest
result = regexp(tmp,' ','split');
result = cat(1,result{:});

%# delete temporary array (if you want)
clear tmp

礼貌:阅读txt文件Matlab的

可能有人请告诉我,安排信息的最佳方式是什么?谢谢你,帮助是多少AP preciated。

Could someone please tell me the best way to arrange the information? Thanks, help is much appreciated.

推荐答案

从code来看,你为什么不使用

Judging from the code, why don't you use

fid = fopen(filename,'rt');
tmp = textscan(fid, '%s %d %d %d %d', 'Headerlines', 10);
fclose(fid);

textscan 使用空间和换行符作为默认分隔符。如果你给换行作为分隔符明确,你失去的空间分隔符,和可移植性(视窗经常使用 \\ r \\ n 作为一个单一的换行符,而Unix的衍生操作系统使用 \\ n )。因此,考虑你的数据,只是离开它。

textscan uses space and newline as delimiters by default. If you give newline as delimiter explicitly, you loose the space as delimiter, and the portability (Windows often uses \r\n as a single newline, whereas Unix-derived OSes use \n). So, given your data, just leave it out.

然后你赴汤蹈火,除去10 headerlines,而 textscan 已经有一个很好的烤机选项。这样,不需要这些步骤。您可以通过正则表达式继续由一通分裂的东西用作为分隔符的空间,但由于 textscan 已分割空间,那也不需要。

Then you jump through hoops to remove 10 headerlines, while textscan already has a nice baked-in option for that. So, those steps aren't needed. You proceed by splitting the stuff by a pass through regexp with a space as delimiter, but since textscan already splits on space, that's not needed either.

所以,使用上面的三条线,您将获得

So, using the three lines above, you'll get

tmp = 
    {9x1 cell}    [9x1 int32]    [9x1 int32]    [9x1 int32]    [9x1 int32]

现在,现在来存储数据更方便。我能想到的两种方式:

Now, now to store the data more conveniently. I can think of two ways:


  1. 电池阵列

  2. 结构

对于这两个,你必须先找到唯一名称:

For both, you'll have to find the unique names first:

[names, inds] = unique(tmp{1});

使用单元阵列

这会给你按名称排序的数据的单元阵列:

Using cell arrays

This will give you a cell-array of the data sorted by name:

data = [tmp{2:end}];
results = arrayfun(@(x) data(strcmp(tmp{1},x),:), ...
            names, 'uniformoutput', false);

现在你可以索引到结果如下:

Now you can index into results as follows:

results{3}(1,4)   %# for the 4th '11' for 'Name3' 

记住,Matlab的是基于1,使 A(3)表示 A 的第三个要素, 不可以 4。

Remember that Matlab is 1-based, so that a(3) indicates the 3rd element of a, not the 4th.

该命令的故障:


  1. 功能 arrayfun 循环通过输入数组中的元素,适用于每个元件的功能,并收集在任一规则阵列的结果(如果可能的)或细胞阵列(时不可能的(错误),并且当给定的'uniformoutput',假)。这是一个有点像的foreach -construct。

  1. The function arrayfun loops through the elements of the input array, applies a function to each element, and collects the results in either a regular array (if possible) or a cell-array (when impossible (error) and when given 'uniformoutput', false). It's a bit like a foreach-construct.

以输入数组等于唯一的名称在第一步中发现,关键是在功能应用到每个名字。功能 @(x)的数据(STRCMP(TMP {1},X):)首先找到了给定名称的指标在 TMP {1} 使用(包含所有名称数组) STRCMP 。这些指数则用于索引数据= [TMP {2}结束] ,即所有其他的阵列。

Taking the input array equal to the unique names found in the first step, the trick is in the function to apply to each name. The function @(x) data(strcmp(tmp{1},x),:) first finds the indices for the the given name in tmp{1} (array containing all names) using strcmp. These indices are then used to index data = [tmp{2:end}], i.e., all the other arrays.

每一个人唯一的名称,结果将存储单元阵列中的结果

The results for each individual unique name is then stored in the cell-array results.

您可以更进一步,并使用电池阵列结果有一个更可读的数据结构。将所有的previous步骤后,执行此:

Using Structures

You can go one step further and use the cell-array results to have a more human-readable data structure. After applying all the previous steps, execute this:

for ii = 1:numel(names)
    output.(names{ii}) = results{ii}; end

现在,你可以通过名字引用您的数据:

Now you can reference to your data by name:

output.Name3(1,4)   %# to index the 4th '11' from 'Name3'

语法 your_struct。('someString')被称为动态结构引用的。它引用的或结构 your_struc 名为 someString 创建的一个领域。

The syntax your_struct.('someString') is called dynamic structure referencing. It references or creates a field in the structure your_struc called someString.

现在,如果 {名二} 包含强调要摆脱,那么你可以定义

Now, if names{ii} contains underscores you want to get rid of, then you can define

camelCase = @(x) regexprep(x, '_+(\w?)', '${upper($1)}')

camelCase = @(x) regexprep(x, ' +(\w?)', '${upper($1)}')

有空格。然后使用

for ii = 1:numel(names)
    output.( camelCase(names{ii}) ) = results{ii}; end

荣誉给这些家伙为最后一个。

这篇关于Matlab的阅读txt文件到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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