Suppose I have a plaintext file test.dat:
foo bar baz
qux ham spam
I know want to load this into Octave (or Matlab if necessary) as a two-dimensional cell array, preserving the structure encoded in whitespace and newlines. According to my understanding of the documentation, the following should be the way to go:
format = '%s';
file = fopen('test.dat');
data = textscan(file,format);
fclose(file);
disp(data);
However this only loads the data as a one-dimensional array:
{
[1,1] =
{
[1,1] = foo
[2,1] = bar
[3,1] = baz
[4,1] = qux
[5,1] = ham
[6,1] = spam
}
}
Explicitly specifying Delimiter, Whitespace, and EndOfLine does not help (what’s the point of the latter then?); neither does using other loading functions like textread or dlmread. What does work is using format = '%s%s%s' in the above but this requires that I somehow identify the number of columns, which the function should be able to do itself.
Thus I ask: Is there any built-in function that does what I want? I am not interested in ways to write such a function myself – I am confident that I can do this, but that’s exactly what I want to avoid (as I need to use this for demonstrating good practice, and thus not re-inventing the wheel).
Related Q&As (that all work with knowing the number of columns):
%sas a format,textscanwill treat the whole line as one string, so yes you do need to know the number of columns. Your only other option is to scan each line at a time usingfgetland then parse the resulting line using whatever separator you have to split each line into separate strings.%sas a format,textscanwill treat the whole line as one string – No, it doesn’t. It loads each of the six elements individually; just the arrangement gets lost.