none
How to put a numeric chart from a file into a Double variable (VB) RRS feed

  • Question

  • I'm coding in Visual Basic and I have a .txt file which looks like this: 

    0.00000 3187.47070 10000.00000 1.36127 50.00000 3119.40698 9984.39355 1.36629 100.00000 3051.09253 9968.78418 1.37153 150.00000 2982.51636 9953.17285 1.37669 200.00000 2913.68188 9937.55859 1.38176 250.00000 2844.59375 9921.94141 1.38671 300.00000 2775.25854 9906.32129 1.39148 350.00000 2705.68457 9890.69922 1.39604 400.00000 2635.88232 9875.07422 1.40034

    ...and so on

    And I need to put that into a Nx4 chart array

    Dim Tab(N, 4) As Double
    What is the simplest way to read those values and put into the array elements?

    Wednesday, June 28, 2017 3:03 AM

All replies

  • Hi friend,

    I'll move it to VB forum for you to get a professional and quick answer.

    Thanks for your understanding.

     

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Fletch Zhou Thursday, June 29, 2017 3:17 AM
    Thursday, June 29, 2017 3:17 AM
  • What is the simplest way to read those values and put into the array elements?

    The TextFieldParser is designed to do what you want. See

    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser%28v=vs.110%29.aspx

    A list of custom objects will be much easier to use than an array, as you do not need to know the size it will be, and it appears likely that you want to keep each line of data available as a single reference. An example would be a dictionary where the first item is the key.  From your example it appears that you will use a fixed width type with white space trimmed.

    If you have to use an array then you can redim it as required.

    Thursday, June 29, 2017 3:26 AM
  • Hi Brunhahn,

    Based on your description, I do one simple sample that you can refer to:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim filepath = "D:\test\test.txt"
            Dim fStream As New System.IO.FileStream(filepath, IO.FileMode.Open)
            Dim sReader As New System.IO.StreamReader(fStream)
    
            Dim List As New List(Of Double())
            Do While sReader.Peek >= 0
                Dim line As String = sReader.ReadLine
                Dim s() As String
                Dim s1(3) As Double
                s = line.Split(" ")
                Dim len As Integer = s.Length
                For i As Integer = 0 To len - 1
                    If s(i) <> "" Then
                        For j As Integer = 0 To 3
                            s1(j) = Convert.ToDouble(s(i))
                        Next
                    End If
                Next
                List.Add(s1)
            Loop
            'to go back to an array
            Dim thisArray As Double()() = List.ToArray
    
            fStream.Close()
            sReader.Close()
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, June 29, 2017 3:55 AM
    Moderator
  • Bruno,

    I missed that you want to use double - in mine shown below, I'm using decimal but it should be an easy fix.

    I agree with Acamar on what do other than using a TextFieldParser. Normally I think that's a great tool but I can't tell if you have those numbers as fixed-width, tab-delimited, or maybe even just padded with space characters?

    I took what you show above and intentionally skewed it some just to be sure that the code that I'll show can handle the empty lines/additional spaces. I have it in a text file on my desktop named "Numbers.txt":

         0.00000   3187.47070   10000.00000      1.36127
    50.00000   3119.40698    9984.39355      1.36629
    100.00000   3051.09253    9968.78418      1.37153
    
    
    150.00000   2982.51636    9953.17285      1.37669
    200.00000   2913.68188    9937.55859      1.38176
    250.00000   2844.59375    9921.94141      1.38671
    300.00000   2775.25854    9906.32129      1.39148
    350.00000   2705.68457    9890.69922      1.39604
    400.00000   2635.88232    9875.07422      1.40034
    
    

    In the following, change "Decimal" to "Double" then the rest should work for you:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.IO.Path
    
    Public Class Form1
        Private _desktop As String = _
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim filePath As String = _
                Combine(_desktop, "Numbers.txt")
    
            Dim test As IEnumerable(Of NumericData) = _
                NumericData.GetData(filePath)
    
            Stop
    
        End Sub
    End Class
    
    
    
    
    
    Public Class NumericData
        Private _columnA As Decimal
        Private _columnB As Decimal
        Private _columnC As Decimal
        Private _columnD As Decimal
    
        Private Sub New(ByVal columnA As Decimal, _
                        ByVal columnB As Decimal, _
                        ByVal columnC As Decimal, _
                        ByVal columnD As Decimal)
    
            _columnA = columnA
            _columnB = columnB
            _columnC = columnC
            _columnD = columnD
    
        End Sub
    
        Public Shared Function _
            GetData(ByVal dataFilePath As String) As IEnumerable(Of NumericData)
    
            Dim retVal As IEnumerable(Of NumericData) = Nothing
    
            If Not String.IsNullOrWhiteSpace(dataFilePath) Then
                Dim fi As New FileInfo(dataFilePath)
    
                If fi.Exists Then
                    Dim tempList As New List(Of NumericData)
    
                    Using rdr As New StreamReader(fi.FullName)
                        Do While rdr.Peek() >= 0
                            Dim stringData() As String = _
                                rdr.ReadLine.Split(New String() {" "}, _
                                    StringSplitOptions.RemoveEmptyEntries)
    
                            If stringData IsNot Nothing AndAlso stringData.Length = 4 Then
                                Dim a, b, c, d As Nullable(Of Decimal)
    
                                If Decimal.TryParse(stringData(0), New Decimal) Then
                                    a = CDec(stringData(0))
                                End If
    
                                If Decimal.TryParse(stringData(1), New Decimal) Then
                                    b = CDec(stringData(1))
                                End If
    
                                If Decimal.TryParse(stringData(2), New Decimal) Then
                                    c = CDec(stringData(2))
                                End If
    
                                If Decimal.TryParse(stringData(3), New Decimal) Then
                                    d = CDec(stringData(3))
                                End If
    
                                If a.HasValue AndAlso _
                                    b.HasValue AndAlso _
                                    c.HasValue AndAlso _
                                    d.HasValue Then
    
                                    tempList.Add(New NumericData(a.Value, b.Value, c.Value, d.Value))
                                Else
                                    ' Although not shown here, at this point you might
                                    ' want to log this entry by also having a line
                                    ' counter variable and creating a collection of
                                    ' line numbers that failed this test.
                                End If
                            End If
                        Loop
                    End Using
    
                    If tempList.Count > 0 Then
                        retVal = tempList.ToArray
                    End If
                End If
            End If
    
            Return retVal
    
        End Function
    
        Public ReadOnly Property ColumnA As Decimal
            Get
                Return _columnA
            End Get
        End Property
    
        Public ReadOnly Property ColumnB As Decimal
            Get
                Return _columnB
            End Get
        End Property
    
        Public ReadOnly Property ColumnC As Decimal
            Get
                Return _columnC
            End Get
        End Property
    
        Public ReadOnly Property ColumnD As Decimal
            Get
                Return _columnD
            End Get
        End Property
    End Class

    You might also want to change the names - that's up to you.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Thursday, June 29, 2017 5:57 PM