将.csv文件调入八度 [英] Calling .csv file into Octave

查看:219
本文介绍了将.csv文件调入八度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的代码,它输出一个具有三列数据(时间,力,高度)的.csv文件。我想使用Octave绘制数据,或者使用C ++文件中的八度函数绘图(我知道这是可能的,但我不一定需要这样做)。



现在我有简单的.m文件:

  filename = linear_wave_loading.csv; 
M = csvread(filename);

只是练习将这个文件带入八度得到这个错误。

 错误:dlmread:错误解析范围

将.csv文件加载到八度的正确方法是什么?


$ b

编辑:这是我的.csv文件的前几行。

 波长= 88.7927 m 
时间高度力(KN / m)
0 -20 70668.2
0 -19 65875
0 -18 61411.9
0 -17 57256.4

感谢您的帮助。

解决方案

使用八度3.8.2

 >> format long g 
>> dlmread('test.csv','',2,0)
ans =

0 0 0 -20 70668.2
0 0 0 -19 65875
0 0 0 -18 61411.9
0 0 0 -17 57256.4

一般,使用dlmread值分隔符不是逗号。此外,您必须跳过两个标题。
理论dlmread使用制表符分隔的值太小了'\t',但是这失败了你给出的例子,因为不连续的标签大小(也许它只是一个复制粘贴问题),因此以一个空格''作为分隔符是一种解决方法。
您应该更好地保存您的.csv文件逗号分隔

  Wavelength = 88.7927 m 
时间高度力KN / m)
0,-20,70668.2
0,-19,65875
0,-18,61411.9
0,-17,57256.4


然后你可以轻松地做 dlmread('file.csv',',',2,0)



您可以试试我的 csv2cell (不要与io package!csv2cell混淆)函数(从未尝试过< 3.8.0)。

 >> str2double(reshape(csv2cell('test.csv','+',2),3,4))'
ans =

0 -20 70668.2
0 -19 65875
0 -18 61411.9
0 -17 57256.4

自动地,但是在空间隔离器的情况下,它经常失败,所以你必须重新塑造你的自我(并在任何情况下转换为双重)。



需要您的标题

 >> reshape(csv2cell('test.csv','+',1),3,5)'
ans =
{
[1,1] = Time
[2 ,1] = +0
[3,1] = +0
[4,1] = +0
[5,1] = +0
[1,2 ] = Height
[2,2] = -20
[3,2] = -19
[4,2] = - 18
[5,2] 17
[1,3] = Force(KN / m)
[2,3] = 70668.2
[3,3] = 65875
[4,3] = 61411.9
[5,3] = 57256.4
}

一切都是您单元格中的字符串。


I have a code written in C++ that outputs a .csv file with data in three columns (Time, Force, Height). I want to plot the data using Octave, or else use the octave function plot in the C++ file (I'm aware this is possible but I don't necessarily need to do it this way).

Right now I have the simple .m file:

filename = linear_wave_loading.csv;
M = csvread(filename);

Just to practice bringing this file into octave (will try and plot after) I am getting this error.

error: dlmread: error parsing range

What is the correct method to load .csv files into octave?

Edit: Here is the first few lines of my .csv file

Wavelength= 88.7927 m
Time    Height  Force(KN/m)
0   -20 70668.2
0   -19 65875
0   -18 61411.9
0   -17 57256.4

Thanks in advance for your help.

解决方案

Using octave 3.8.2

>> format long g
>> dlmread ('test.csv',' ',2,0)
ans =

                     0                     0                     0                   -20               70668.2
                     0                     0                     0                   -19                 65875
                     0                     0                     0                   -18               61411.9
                     0                     0                     0                   -17               57256.4

General, use dlmread if your value separator is not a comma. Furthermore, you have to skip the two headlines. Theoretical dlmread works with tab separated values too '\t', but this failes with you given example, because of the discontinuous tab size (maybe it's just a copy paste problem), so taking one space ' ' as separator is a workaround. you should better save your .csv file comma separated

Wavelength= 88.7927 m
Time    Height  Force(KN/m)
0, -20, 70668.2
0, -19, 65875
0, -18, 61411.9
0, -17, 57256.4

Then you can easily do dlmread('file.csv',',',2,0).

You can try my csv2cell(not to be confused with csv2cell from io package!) function (never tried it < 3.8.0).

>> str2double(reshape(csv2cell('test.csv', ' +',2),3,4))'
ans =

                 0                   -20               70668.2
                 0                   -19                 65875
                 0                   -18               61411.9
                 0                   -17               57256.4

Usually it reshaped successful automatically, but in case of space seperators, it often failed, so you have to reshape it by your self (and convert to double in any case).

And when you need your headline

>> reshape(csv2cell('test.csv', ' +',1),3,5)'
ans =
{
  [1,1] = Time
  [2,1] = +0
  [3,1] = +0
  [4,1] = +0
  [5,1] = +0
  [1,2] = Height
  [2,2] = -20
  [3,2] = -19
  [4,2] = -18
  [5,2] = -17
  [1,3] = Force(KN/m)
  [2,3] = 70668.2
  [3,3] = 65875
  [4,3] = 61411.9
  [5,3] = 57256.4
}

But take care, then everything is a string in your cell.

这篇关于将.csv文件调入八度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆