matlab - 在一个大文件中用int替换string

时间:2017-05-25 08:55:52

标签: matlab

我有一个像这样的模式文件,

30,402.660064697,196.744171143,18.5563354492,15.3047790527,2.16090902686,aeroplane
30,177.246170044,113.594314575,18.9164428711,16.5203704834,1.71010773629,aeroplane
30,392.224212646,226.437973022,26.2086791992,27.4663391113,0.782454758883,aeroplane
30,241.633453369,169.349304199,628.560913086,540.041259766,0.530623318627,aeroplane
30,529.454589844,322.412719727,24.9837646484,21.5563354492,0.503144180402,aeroplane
30,365.581298828,148.842697144,21.3596801758,16.3081970215,0.490551069379,sheep
30,436.230773926,272.073303223,17.6417236328,19.9946289062,0.483223423362,aeroplane
30,438.188201904,286.455200195,20.164855957,23.1041870117,0.224495329894,adog
30,511.185546875,289.902099609,19.7315673828,19.3796386719,0.203064805828,aeroplane
30,365.777252197,177.576202393,21.8588256836,15.1581115723,0.181338354014,cat
30,380.210266113,150.396713257,19.6742553711,15.7977600098,0.171210919507,aeroplane

和另一个文件包含元数据

dog
aeroplane
cat
sheep

现在我想映射字符串(例如:飞机到int)

dog       -->1
aeroplane -->2
cat       -->3
sheep     -->4

我知道我可以打开一个新文件并按fgets逐行转换 但在我的情况下,这种模式有数千个,这将是非常缓慢的。 有没有更聪明的方法可以解决这个问题,dont need to create a new file and just update in same file

1 个答案:

答案 0 :(得分:0)

假设文件不是太大而无法读入内存,我建议使用textscan和strcomp以及fprintf。如果你真的想要,你可以覆盖现有的文件,但我建议你写一个不同的文件(特别是在调试时)。

相关问题