注册 登录
编程论坛 VB6论坛

求教各位专家,如何读取txt数据并绘制曲线

piaoranyuyun 发布于 2023-06-01 14:21, 1161 次点击
本人纯菜鸟。问题是这样的:txt中有两列数据,类似下面的样式。想前面一列数据作为纵坐标,后面一列数据作为横坐标。坐标原点在左下角,X轴范围为0到0.1,Y轴范围为0到1000。如何编写代码啊?想用picture box或者新的窗口来输出图像。万分感谢!
235.000000      0.003119
237.800000      0.003340
240.600000      0.003579
243.400000      0.003839
246.200000      0.004120
249.000000      0.004425
251.800000      0.004755
254.600000      0.005111
257.400000      0.005496
260.200000      0.005913
263.000000      0.006362
265.800000      0.006847
268.600000      0.007369
271.400000      0.007932
274.200000      0.008538
277.000000      0.009190
279.800000      0.009892
282.600000      0.010646
285.400000      0.011455
288.200000      0.012324
291.000000      0.013256
293.800000      0.014256
296.600000      0.015326
299.400000      0.016473
302.200000      0.017700
305.000000      0.019012
307.800000      0.020414
310.600000      0.021913
313.400000      0.023513
316.200000      0.025220
319.000000      0.027041
321.800000      0.028982
324.600000      0.031050
327.400000      0.033252
330.200000      0.035596
333.000000      0.038089
335.800000      0.040741
338.600000      0.043558
341.400000      0.046551
344.200000      0.049728
347.000000      0.053100
349.800000      0.056676
352.600000      0.060468
355.400000      0.064486
358.200000      0.068743
361.000000      0.073250
363.800000      0.078019
366.600000      0.083065
369.400000      0.088401
372.200000      0.094041
375.000000      0.100000
8 回复
#2
cwa99582023-06-02 09:25
把数据保存在txt文件里。
打开vb6,在窗体上放置两个按钮,一个pictureBox控件,拷贝下面的代码。

Private Sub Command1_Click()
    Dim Xx() As Single, Yy() As Single, i As Integer
    Dim St As String
   
    Open App.Path & "\数据.txt" For Input As #1
   
    Do While Not EOF(1)
        ReDim Preserve Xx(i), Yy(i)
        Line Input #1, St
        St = Replace(St, "      ", " ")  '删除多余的空格,只留一个
        Xx(i) = Split(St)(1)
        Yy(i) = Split(St)(0)
        i = i + 1
        
    Loop
    Close
   
    Call HuiTu(Xx(), Yy())
   
End Sub

Private Sub Command2_Click()
    End
End Sub

Sub HuiTu(x() As Single, y() As Single)
   
    Picture1.Scale (-0.01, 1000)-(0.11, -100)
    Picture1.Cls
    Picture1.ForeColor = vbBlack
    Picture1.DrawWidth = 1
    Picture1.Line (-0.01, 0)-(0.11, 0)
    Picture1.Line (0, -100)-(0, 1000)
    Picture1.Print " 1000"
    Picture1.CurrentX = 0
    Picture1.CurrentY = 0
    Picture1.Print " 0";
    Picture1.CurrentX = 0.103
    Picture1.Print "0.1"
   
    Picture1.ForeColor = vbRed
    Picture1.DrawWidth = 2
   
    For i = 0 To UBound(x)
   
        Picture1.PSet (x(i), y(i))
    Next
   
End Sub
#3
piaoranyuyun2023-06-02 11:03
回复 2楼 cwa9958
感谢大神回复。我尝试着运行,提示以下错误。
实时错误9,下标越界。点击调试,提示下面一行有问题,麻烦再帮忙看看啊。感激!
Xx(i) = Split(St)(1)
只有本站会员才能查看附件,请 登录
#4
cwa99582023-06-02 15:14
那是你的文件有问题
打开数据文件看看,两个数据中间有没有空格。
用我的文件看看。
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2023-6-2 15:16编辑过]

#5
piaoranyuyun2023-06-02 16:25
回复 4楼 cwa9958
还真的是!我把txt文件换成你给的就行,换回我的文件就不行。不知道为啥啊?我把代码附上。你看看是不是我输出txt的代码有问题啊?
还有,如果我的坐标轴需要根据数据的最小值和最大值来定义,应该怎么弄呢?研究了好久,还是不会,希望大神你能帮忙,万分谢谢!
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
Dim c As Double
Dim d As Double
Dim e As Double
Dim f As Double
Dim n As Double
Dim ST As String
Dim SN As String
Dim TS As String
Dim TP As String
Dim PN As String
Dim i As Integer
Dim iMsg1 As String
Dim iMsg2 As String
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
d = Val(TextBox4.Text)
e = Log(((d / 100) - b / c) / 0.002)
f = Log(b / a)
n = e / f
For i = 0 To 50 Step 1   
        ST = FormatNumber(a + (b - a) / 50 * i, 6, vbTrue)
        SN = FormatNumber((ST / c) + (0.002) * (ST / a) ^ n, 6, vbTrue)
        TS = FormatNumber(ST * (1 + SN), 6, vbTrue)
        TP = Log(1 + SN)   
        PN = FormatNumber(TP - TS / c, 6, vbTrue)
        iMsg1 = iMsg1 & ST & Space(6) & SN & vbCrLf
        iMsg2 = iMsg2 & TS & Space(6) & PN & vbCrLf   
       Open App.Path & "/Engineer Stress_Engineer strain.txt" For Output As #1
       Print #1, iMsg1
       Close #1
       Open App.Path & "/True Stress_Plastic strain.txt" For Output As #2
       Print #2, iMsg2
       Close #2
       Next i
End sub

只有本站会员才能查看附件,请 登录
#6
风吹过b2023-06-02 20:27
绘制曲线参考下吧
https://bbs.bccn.net/thread-306299-1-1.html#pid1771129

下标报错,是因为读到了空行或者不包含空格的行,判断一下吧。
        Line Input #1, St
  if instr(1,str," ") >1 then         '最少有二个字符,空格不能在第1位

        St = Replace(St, "      ", " ")  '删除多余的空格,只留一个
        '另外上面这句,为了适用不同的空格,建议写一个 do 循环来连续替换掉空格
        'do
        '   l=len(st)        '取初始长度
        '   st=replace(st,"  "," ")     '2个空格换成1个空格
        'loop len(st)<>l     '如果替换后长度不等于替换前的长度,说明本次替换成功,继续循环替换。
                             '如果替换后长度等于替换前的长度,说明本次搜索到2个空格的情况,那么结束替换

        Xx(i) = Split(St)(1)
        Yy(i) = Split(St)(0)
        i = i + 1
   end if
    Loop

浏览器内手写的代码,未经测试。

#7
piaoranyuyun2023-06-03 12:16
回复 6楼 风吹过b
太感谢了!好人一生平安,阖家幸福安康!测试没有问题了。
#8
cwa99582023-06-05 08:51
回复 5楼 piaoranyuyun
额额,那是你的文件的末尾有两个空行,所以会出错。
对于你的这个文件,可以加一句判断就可以。具体问题要具体分析。

    Do While Not EOF(1)
        ReDim Preserve Xx(i), Yy(i)
        Line Input #1, St
        St = Replace(St, "      ", " ")  '删除多余的空格,只留一个
        If St <> "" Then
            Xx(i) = Split(St)(1)
            Yy(i) = Split(St)(0)
            i = i + 1
        End If
    Loop
#9
piaoranyuyun2023-06-05 12:55
回复 8楼 cwa9958
感谢回复!已经解决了!
1