Calling .csv file into Octave

我的梦境 提交于 2019-12-04 19:32:21

问题


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.


回答1:


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.




回答2:


Your not storing you .csv filename as a string. Try:

filename = 'linear_wave_loading.csv';


来源:https://stackoverflow.com/questions/25325577/calling-csv-file-into-octave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!