1

How can I load a string array in Octave from a csv or txt file? The file has the format seen below. When using

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

it results in useless data like 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 Answers 3

1

From the documentation on dlmread (emphasis by me):

Read numeric data from the text file file which uses the delimiter sep between data values.

Use textscan instead (depending on your OS, the delimiter might needs to be modified):

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

Output:

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
  }
}

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Similar to textscan, except it preserves empty lines.

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

Comments

0

No need for ugly textscan shenanigans.

Just use the csv2cell function from the io package.
I've used it many times, works like a charm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.