Matlab导入带有标题和浮点精度数字的CSV文件

时间:2012-11-27 19:38:52

标签: matlab csv import floating-point-precision

我的问题是将CSV文件中的数字导入到matlab变量,其精度与原始数字相同。这是我的数据示例(可以有X个行,文件顶部有一个标题):

g_B_esti_Velx, g_B_Velx, g_B_ACCx, g_B_esti_Vely, g_B_Vely, g_B_ACCy, g_B_esti_Velz, g_B_Velz, g_B_ACCz, ControlPitch, AnglePitch, ControlRoll, AngleRoll, Controlyaw, AngleYaw, Controlthrust, g_throttle, msgbatteryVoltage, msgcurrent, msgcapacity_mAh, time, index, tracking, detected
-0.266192,-1.072520,0.788713,0.163473,0.623330,-0.333946,0.000000,-0.773519,0.451613,7,-0.050615,0,-0.017453,0,-0.331613,128,472,142,97,2227,1340731788.614960,179,1,1
-0.270800,-0.486626,0.876130,0.125808,0.329368,-1.245129,0.000000,-0.459128,0.193548,40,-0.033161,-35,-0.017453,0,0.558505,128,472,142,99,2227,1340731788.715275,180,1,1

我已使用导入向导和以下代码成功导入数据。但问题是数字导入只有双精度,这还不够。

fid = fopen('mk-2012-6-26-19-29.csv');
mydata = textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','Delimiter',',','CollectOutput',1);
fclose(fid);
mydata = [cellfun(@str2double,[mydata{1:end}])]';
whos mydata

和matlab导入向导结果:

newData1 = importdata('mk-2012-6-26-19-29.csv');
for i = 1:size(newData1.colheaders, 2)  % variable count
    assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i)); %limit range on first parameter with :
end

我正在使用Matlab R2011a

1 个答案:

答案 0 :(得分:1)

问题似乎只有我的Matlab显示精度

以下命令允许我检查:

>> format long
>> time_mk(1,1)

ans =

     1.340731788614960e+09

结果与我问题上的第一行时间变量匹配。