1

これは本当に簡単な質問ですが、私がどれほど立ち往生しているか信じられません。

各列が異なる形式のファイルがあります。一部は文字列で、一部は int です。textscan を使用することで、ファイルをセルのベクトルとして MATLAB に取り込むことができます。

私がやりたいことは、10 歳未満の猫だけが含まれる新しいマトリックスを作成することです。

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

個々の細胞にいるという事実がなければ、これは問題にはなりません。A{1} は動物のリストを、A{2} は年齢のリストを返します。ファイルを読み取る方法はそれほど重要ではないため、 textscan を使用しないことで解決できる場合は、それもオプションです。

4

1 に答える 1

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);
于 2013-11-06T17:34:23.963 に答える