如何将.csv转换为.txt进行Matlab导入

时间:2017-08-09 13:30:12

标签: vba excel-vba excel

我有一个.csv文件。我想将它转换为.txt文件,从A行开始到B行(A,B在开头声明)。 .txt文件应该有差距" "而不是原始的分号。此外,原始.csv文件中每个复制行的结尾应该用新的.txt文件中的分号表示(txt文件将用作Matlab的输入矩阵)。

这就是我现在所拥有的,但它无法正常工作

   Sub csv2mat()
        Dim Filename As String
        Dim Filenamenew As String
        'Change the path to the Files and and create a txt file
        Filename = "C:\Documents and Settings\user\Desktop\as.csv"
        Filenamenew = "C:\Documents and Settings\user\Desktop\new.txt"
        Open Filenamenew For Output As #2
        Open Filenamme For Input As #1
        Do While Not EOF(1)
            Line Input #1, str
            str = Replace(str, ";", """")
            Print #2, str
        Loop   
        End Sub

1 个答案:

答案 0 :(得分:1)

你喜欢像Filnemme那样的一些拼写错误。 而你忘了在阅读后关闭文件。 你没有添加&#34 ;;"如果我理解你的话,广告和字符串

试试这个:

Sub csv2mat()
        Dim Filename As String
        Dim Filenamenew As String
        Dim str As String
        'Change the path to the Files and and create a txt file
        Filename = "C:\Documents and Settings\user\Desktop\as.csv"
        Filenamenew = "C:\Documents and Settings\user\Desktop\new.txt"
        Open Filenamenew For Output As #2
        Open Filename For Input As #1
        Do While Not EOF(1)
            Line Input #1, str
            str = Replace(str, ";", """") & ";"
            Print #2, str
        Loop
        Close #1
        Close #2
        End Sub