简体   繁体   中英

Properly getting X and Y coordinates from a text file and plot the curve

I have a tab-delimited file containing numbers in scientific notation in 2 separate columns (cf. picture). I need to properly read the file, store the first column as the X coordinates and the second to Y (after having converted the separated strings into double or float), and then plot Y as function of X (in my case power as function of frequency). I am new in C# and I have tried to follow previous posts unsuccessfully. How can I do this? 在此处输入图片说明

You can do something like this (providing that the separator between columns is tabulation \\t ):

var data = File
  .ReadLines(@"C:\test_.txt")
  .Skip(1)
  .Select(line => line.Split(new Char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries))
  .Select(chunk => chunk
    .Select(x => Double.Parse(x, CultureInfo.InvariantCulture))
    .ToArray());

foreach (var pair in data) 
  myChart.Series[0].Points.AddXY(pair[0], pair[1]);

@ Dmitry: I have finally found this Toolbox, I had problems finding it as I am working with a French visual Studio. I did what you said but it is still not recognizing myChart. Here is a screenshot of Visual Studio. Visual Studio screenshot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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