从文件加载字符串数组

时间:2019-11-21 09:23:29

标签: octave

如何从csvtxt文件在Octave中加载字符串数组?该文件具有如下所示的格式。使用

dlmread("TimeData.csv", ",")

它会导致无用的数据,例如2019 - 11i

`TIME`
    2019-11-08-13.27.03 +0100
    2019-11-08-13.27.08 +0100
    2019-11-08-13.27.13 +0100
    2019-11-08-13.27.18 +0100
    2019-11-08-13.27.23 +0100

3 个答案:

答案 0 :(得分:1)

摘自dlmread上的文档(我强调):

  

从文本文件 file 中读取数字数据,该文件在数据值之间使用定界符 sep

改为使用textscan(根据您的操作系统,定界符可能需要修改):

fid = fopen('TimeData.csv');
C = textscan(fid, '%s', 'Delimiter', '\n')
fclose(fid);

输出:

C =
{
  [1,1] =
  {
    [1,1] = 2019-11-08-13.27.03 +0100
    [2,1] = 2019-11-08-13.27.08 +0100
    [3,1] = 2019-11-08-13.27.13 +0100
    [4,1] = 2019-11-08-13.27.18 +0100
    [5,1] = 2019-11-08-13.27.23 +0100
  }
}

希望有帮助!

答案 1 :(得分:0)

类似于textscan,除了保留空行。

function [textLines] = readFile (file_in_dir)
    if nargin != 1 || isempty(file_in_dir)
        return
    end

    if exist(file_in_dir, "file")
        fid = fopen(file_in_dir, "r");
        data = char(fread(fid));
        fclose(fid);

        if !strcmp(data(end), "\n")
            data = [data ; "\n"];
        end

        data(data == "\r") = [];
        newlines = [0, [1 : rows(data)](data' == "\n")];

        textLines = cell(1, columns(newlines) - 1);
        for i = 1 : columns(newlines) - 1
            textLines{i} = data(newlines(i) + 1 : newlines(i + 1) - 1)';
        end
    else
        textLines = {};
    end
end

答案 2 :(得分:0)

不需要丑陋的textscan恶作剧。

只需使用csv2cell软件包中的io函数。
我已经使用过很多次了,就像一个护身符。