在matlab中调用非格式化文本文件中的特定数字

时间:2015-08-14 14:31:20

标签: matlab textscan

我有一个文本文件,其中包含:

// MATLAB to process this file
ModelName : Matlab1
PerturbationId : 0
X1X2_001 : 0.270971584968118
X1X2_002 : 0.55555
OBJECTF : ?
NumberOfRuns : 25
SimulationId : 0

我想要的是得到0.270971584968118和0.55555,重要的是每次运行后文件不一样,我的意思是X1X2的名称可能会改变(但总有001和002)结束)。值也可能会改变(0.270971584968118和0.55555)。 我使用了文本扫描,但由于文件没有特定的格式(值没有用制表符分隔,并且它们不是表格式的),所以它没用。

由于

1 个答案:

答案 0 :(得分:0)

OKAY。所以直截了当的方式说:

%//read in file
fileID = fopen('your.txt')
C = textscan(fileID,'%s %s','Delimiter',':');  %// delimit on ':'
%//you are looking for C{2} the second row of the cell.
value1 = cell2mat(C{2}(4,:));
>> 0.270971584968118
value2 = cell2mat(C{2}(5,:));
>> 0.55555

所以这很简单易懂,但并不是最有效的方法。为了提高效率,请寻找cell2mat的替代方案。