选项 1:
您可以将textscan 与以下formatSpec 一起使用:
formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
data = textscan(fileID,formatSpec,'Delimiter',',');
其中fileID是fopen获取的文件标识符。
选项 2:
另一种选择是使用readtable,具有上述格式(直接使用文件名,没有文件ID):
data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
'ReadVariableNames',false);
% the next lines are just to give the table variables some meaningful names:
varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
data.Properties.VariableNames = varNames(2:end-1);
结果(忽略值,因为我在玩这个例子时有点搞砸了):
data =
4×5 table
correct time guess action file
_______ ______ _______________ ______ ____
'yes' 6.8888 'Lay' 'Lay' 16
'no' 7.8762 'Put on top' 'Stir' 18
'no' 7.1503 'Put on bottom' 'Stir' 3
'no' 7.151 'go' 'Stir' 270
选项 2 的优点是表是 a much more convenient way 来保存这些数据,而不是元胞数组(这是 textscan 的输出)。