从单元格向量中提取行

时间:2013-11-06 16:54:32

标签: matlab row cell

这是一个非常简单的问题,但我无法相信我是多么困难。

我有一个文件,其中每列是不同的格式。有些是字符串,有些是int。 通过使用文本扫描,我可以将文件作为单元格向量传入MATLAB。

我想要做的是创建一个只有10岁以下的猫才能进入的新矩阵。

Animal Age
Cat 11
Dog 5
Cat 2
Dog 11
Cat 16
Cat 3

如果不是因为他们在他们的个体细胞中,这不会是一个问题。一个{1}给了我一个动物列表,A {2}给了我一个年龄列表。如果可以通过不使用文本扫描来解决这个问题,那么阅读文件的方式并不重要。

1 个答案:

答案 0 :(得分:3)

以下是我将如何做到这一点

 FileId2=fopen('Animals.txt')
 title=textscan(FileId2,'%s %s',1);   % this reads only the first line 
 data==textscan(FileId2,'%s %d');     % reads the rest of the file and stores them in string and in
 Cat=strcmp(D{1},'Cat');              % looks for Animal of type cat
 Age=data{2};                         % gets the age of the animal
 Ageofcats=Age(Cat);                  % get the age of cats 
 % and then you just find what you want
 find(Ageofcats<10);