简体   繁体   English

从文本文件中正确获取 X 和 Y 坐标并绘制曲线

[英]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).我有一个制表符分隔的文件,其中包含 2 个单独列中的科学记数法数字(参见图片)。 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).我需要正确读取文件,将第一列存储为 X 坐标,将第二列存储为 Y(将分离的字符串转换为双精度或浮点数后),然后将 Y 绘制为 X 的函数(在我的情况下为频率)。 I am new in C# and I have tried to follow previous posts unsuccessfully.我是 C# 的新手,我试图按照以前的帖子失败。 How can I do this?我怎样才能做到这一点? 在此处输入图片说明

You can do something like this (providing that the separator between columns is tabulation \\t ):您可以执行以下操作(前提是列之间的分隔符是制表\\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. @ Dmitry:我终于找到了这个工具箱,我在使用法国视觉工作室时遇到了问题。 I did what you said but it is still not recognizing myChart.我按照你说的做了,但它仍然无法识别 myChart。 Here is a screenshot of Visual Studio.这是 Visual Studio 的屏幕截图。 Visual Studio screenshot Visual Studio 屏幕截图

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

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