matlab 定界符不起作用

matlab delimiters doesn't work

我有一个文件 test.txt 看起来像:

日期时间季节假期工作日天气温度湿度

2011/1/1 0:00 1 0 0 1 冷高
2011/1/1 1:00 1 0 0 1 冷高

我想通过分隔符“\t”将它们导入到matlab中。但是,a=importdata('test.txt','\t') 只是将数据作为一个整体导入,而不是以\t 分隔。

我也尝试其他命令,如 dlmread:

使用 dlmread 时出错(第 139 行) 文件和格式字符串不匹配。 无法从文件中读取数字(行 1u,字段 1u)==> 日期时间季节假期工作日天气温度 humidity\n

然后我尝试 str1=textsacn('test.txt','%s%s%s%s%s%s%s');

未定义函数 'textsacn' 用于 'char' 类型的输入参数。

我的matlab版本是2012a。我使用 windows。 有人可以帮忙吗?

importdatadlmread 用于数值数据。对于混合 text/numerical,您必须使用 textscan(您在 textsacn 上有错字)与 fopenfclose,然后使用 reshape:

fid = fopen('test.txt');
s = textscan(fid,  '%s', 'Delimiter', '\t');
fclose(fid);

s = reshape(s{1}, [7 numel(s{1})/7])';

和输出:

s = 
    '2011/1/1 0:00'    '1'    '0'    '0'    '1'    'cold'    'high'
    '2011/1/1 1:00'    '1'    '0'    '0'    '1'    'cold'    'high'

然后,一些字符串如'0''1'可以用str2num转换为数字。

最佳,

也可以使用textscan,直接指定数字格式:

fid = fopen('test.txt');
out = textscan(fid, '%s%d%d%d%d%s%s', 'Delimiter', '\t')
fclose(fid);

这样,变量 out 已经包含了四个数字列的数字。您可以通过执行以下操作将第一列转换为 datenum:

dates = datenum(out{1});