我正在尝试通过文本文件将实验收集的数据读入 MATLAB。数据都是整数,分八列,并用空格分隔。
我想打开文件,读入数据,然后重新整形为代表数组。
所以,我有:
fileID = fopen('String name of file');
A = fread(fileid);
B = reshape(A, (length(A) / 8), 8);
默认情况下,fread
假定数据是 double ,但是当我尝试指定整数时,例如 fread(fileid, inf, 'int')
,数据仍然得出错误的值。
数据由 Java 程序输出为“int”,并在 Linux 环境中生成,但现在在 Windows 中(我只有 MATLAB 许可证)。我认为这是为 fread
指定正确数据类型的问题,但我不确定。
有什么想法吗?
最佳答案
我不知道如何使用 fread,但 textscan 应该可以:
% Open file
fid = fopen('file.txt');
% Scan 8 columns of numeric values
tmp = textscan( fid, repmat('%f ', [1 8]));
% Close file handle
fclose(fid);
% Convert to int and join columns into matrix
data = int32(cat(2, tmp{:}));
关于java - 在 MATLAB 中读取文件时指定数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18833915/