简体   繁体   English

将.csv文件调用到Octave

[英]Calling .csv file into Octave

I have a code written in C++ that outputs a .csv file with data in three columns (Time, Force, Height). 我有一个用C ++编写的代码,它输出一个.csv文件,其中包含三列数据(时间,力,高度)。 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). 我想使用Octave绘制数据,或者使用C ++文件中的八度函数图(我知道这是可能的,但我不一定需要这样做)。

Right now I have the simple .m file: 现在我有简单的.m文件:

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? 将.csv文件加载到八度音程的正确方法是什么?

Edit: Here is the first few lines of my .csv file 编辑:这是我的.csv文件的前几行

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 使用八度音程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. 一般情况下,如果您的值分隔符不是逗号,请使用dlmread。 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. 理论dlmread可与制表符分隔值太'\\t' ,但是这failes与你给出的例子,因为不连续的标签尺寸(也许它只是一个复制粘贴问题),所以取一个空格' '作为分隔符是一种解决方法。 you should better save your .csv file comma separated 你应该更好地保存.csv文件逗号分隔

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) . 然后你可以轻松地做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). 您可以尝试我的csv2cell (不要与io包中的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

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). 通常它会自动重新成形,但是如果是空间分隔符,它通常会失败,所以你必须自己重塑它(并且在任何情况下都转换为double)。

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. 但要小心,那么一切都是你牢房里的一根绳子。

Your not storing you .csv filename as a string. 你没有将.csv文件名存储为字符串。 Try: 尝试:

filename = 'linear_wave_loading.csv';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM