matlab读取带格式文本文件
想用matlab读取如下格式的文本文件,文件名为face,下面的格式的举例:1223(_sexmale)(_agechild)(_racewhite)(_facesmil...
想用matlab读取如下格式的文本文件,文件名为face,下面的格式的举例:
1223 (_sex male) (_age child) (_race white) (_face smiling) (_prop '())
1224 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1225 (_sex male) (_age child) (_race white) (_face smiling) (_prop '())
1226 (_sex male) (_age child) (_race white) (_face smiling) (_prop '())
1227 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1228 (_missing descriptor)
1229 (_sex male) (_age adult) (_race white) (_face serious) (_prop '())
1230 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1231 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1232 (_missing descriptor)
1233 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1234 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1235 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1236 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1237 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1238 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
现在的需要是把这些数据转换为一个矩阵A,其中A的第一列为序号(1223,1224,etc);第二列为性别,0代表male,1代表female;第三列为年龄,0代表chile,1代表teen,2代表adult,3代表old;第四列为种族,0代表white,1代表black;第五列为表情,0代表smiling,1代表serious,2代表funny。
这种格式的文件用哪个函数读比较方便呢?如果有代码(只需要拿其中一列举个例子就行)就更好了。 展开
1223 (_sex male) (_age child) (_race white) (_face smiling) (_prop '())
1224 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1225 (_sex male) (_age child) (_race white) (_face smiling) (_prop '())
1226 (_sex male) (_age child) (_race white) (_face smiling) (_prop '())
1227 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1228 (_missing descriptor)
1229 (_sex male) (_age adult) (_race white) (_face serious) (_prop '())
1230 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1231 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1232 (_missing descriptor)
1233 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1234 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1235 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1236 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1237 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
1238 (_sex male) (_age child) (_race white) (_face serious) (_prop '())
现在的需要是把这些数据转换为一个矩阵A,其中A的第一列为序号(1223,1224,etc);第二列为性别,0代表male,1代表female;第三列为年龄,0代表chile,1代表teen,2代表adult,3代表old;第四列为种族,0代表white,1代表black;第五列为表情,0代表smiling,1代表serious,2代表funny。
这种格式的文件用哪个函数读比较方便呢?如果有代码(只需要拿其中一列举个例子就行)就更好了。 展开
展开全部
fid=fopen('路径\face.txt');
a=textscan(fid,'%d %*s %s %*s %s %*s %s %*s %s %*s %*s');
fclose(fid);
data=nan(length(a{1}),5);
data(:,1)=a{1};
data(strcmp(a{2},'male)'),2)=0;
data(strcmp(a{2},'female)'),2)=1;
data(strcmp(a{3},'child)'),3)=0;
data(strcmp(a{3},'teen)'),3)=1;
data(strcmp(a{3},'adult)'),3)=2;
data(strcmp(a{3},'old)'),3)=3;
data(strcmp(a{4},'white)'),4)=0;
data(strcmp(a{4},'black)'),4)=1;
data(strcmp(a{5},'smiling)'),5)=0;
data(strcmp(a{5},'serious)'),5)=1;
data(strcmp(a{5},'funny)'),5)=2;
按你给的数据例子运行结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询