How to import .csv file data into gsl_vectors? C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to import .csv file data into gsl_vectors? C++

I have a .csv file that looks like: X,Y,Z -0.00624347,-0.0182673,1.00063 -0.00845628,-0.0374925,1.00058 -0.00494793,-0.0295639,0.927447 And I want to put it into three gsl_vectors (from GSL:https://www.gnu.org/software/gsl/doc/html/vectors.html) corresponding to the column X, column Y and column Z .I later want to use the functions that are implemented in the GSL on this data. I want to emphasize that these functions can only work on gsl_vectors and not on std:vectors. My approaches: 1. Put the data from the .csv into std:vectors, then transform those vectors into gsl_vectors. That did not work. 2. Put the data from the .csv file into gsl_vectors directly: https://code.sololearn.com/cA2a5A24a104 This gives no output on the Console and if I debug it, the function gsl_vector_set throws an exception: Exception thrown at 0x7A5EED1A (gsld.dll) in progr.exe: 0xC0000005: Access violation writing location 0x3333332, at gsl_set_vector's line: v->data[i * v->stride] = x; 3. Put the .csv data into a gsl_block and then slice it into gsl_vectors: https://code.sololearn.com/ca6a21A24A20 If I run this I get: Debug Error! abort() has been called And on the Console it is shown: gsl: C:\DEV\vcpkg\buildtrees\gsl\src\gsl-2-fb511965d5.clean\block\fprintf_source.c:90: ERROR: fscanf failed Default GSL error handler invoked. If I debug this, the gsl_error function throws an exception: progr.exe has triggered a breakpoint, at abort ();

1st Aug 2021, 1:57 PM
lalal
5 Answers
+ 2
Thank you for the advice! I've shared the links now.
1st Aug 2021, 2:22 PM
lalal
+ 2
it seems there isn't much cpp experts in sololearn it has been hours this question is here :) I am not an expert but according to documentation link you have provided i come up with something like this. there is probability that it can help. if you have found solution please share it here. this gsl seems to be useful in future for me //Read csv into matrix gsl_matrix * data = gsl_matrix_alloc (3, 3); //specify input data dimensions FILE * f = fopen ("data.csv", "rb"); gsl_matrix_fread (f, data ); fclose (f); gsl_vector_view Xv = gsl_matrix_column (data, 0); gsl_vector_view Yv = gsl_matrix_column (data, 1); gsl_vector_view Zv = gsl_matrix_column (data, 2); //Then convert to gsl_vector gsl_vector X = Xv.vector; gsl_vector Y = Yv.vector; gsl_vector Z = Zv.vector;
1st Aug 2021, 4:07 PM
▬▬▬
▬▬▬ - avatar
+ 2
Thank you for your help! I haven't thought of this idea before and it might work! I'm currently debugging since it doesn't show me what I want and I hope I will succeed in order to share it here! Thank you very much once again!
2nd Aug 2021, 10:29 AM
lalal
+ 2
lalal In documentation i think there was another method that you can use to access a column by its header name. In sololearn are you able to run code with gsl lib? when i tried it fails to include gsl lib.
2nd Aug 2021, 10:47 AM
▬▬▬
▬▬▬ - avatar
+ 1
I can't run it in sololearn. Instead, I have installed the library on my local machine like this: https://solarianprogrammer.com/2020/01/26/getting-started-gsl-gnu-scientific-library-windows-macos-linux/
2nd Aug 2021, 11:35 AM
lalal