Ovidiu
Ovidiu

Reputation: 75

Cannot load a file in Octave

Apologies in advance if I get something wrong. This is my first post here (I'm sure there will be many to follow).

I have a file I want to load into Octave but it doesn't work. It is a plain text file (.txt). The file is homogeneous and an excerpt of a few lines from it look like this:

0023,225.935,341.770,17.658
0024,225.935,341.758,17.782
LTAX17,228.152,353.935,17.665
LTAX24,288.304,332.878,24.074

where the first column depicts the name of the point while the rest represent its 3D coordinates.

Some of the options I've tried (but not limited to these) were unsuccessful.

x=load(text.txt)
error: scalar cannot be indexed with .
error: evaluating argument list element number 1

x=load("-text", "text.txt")
warning: load: file found in load path
error: load: empty name keyword or no data found in file 'text.txt'

x=fileread(text.txt)
warning: load: file found in load path
error: load: empty name keyword or no data found in file 'text.txt'

I have also tried simplifying the file, leaving only the coordinates and treating the file as a CSV but I keep getting similar errors.

Upvotes: 5

Views: 26096

Answers (3)

Freddy Pacheco
Freddy Pacheco

Reputation: 1

open a notepad give this format

# Created by Octave 4.2.1, Sat Jun 03 19:48:46 2017 GMT <unknown@Asus>
# name: a
# type: matrix
# rows: 6
# columns: 3
60 30 50
20 45 65
24 23 23
23 32 32
23 65 65
34 54 45

save as file.data

open octave

>>load file.data
>>a

Upvotes: 0

Karan
Karan

Reputation: 1

trying copying your data to Microsoft's wordpad and save the file as a ".dat" extension and load it into Octave. Worked for me, hope the same works for you, good luck!

Upvotes: 0

am304
am304

Reputation: 13876

I think load is only for data files, not for text files. You might to use csvread, dlmread, textscan or textread. Check the documentation as to the correct syntax for invoking each of these functions.

Here are the different approaches:

  1. load does not work as you found out

    x = load('test.txt')

     error: value on right hand side of assignment is undefined
    
  2. csvread works but converts all non-numeric values to 0

    x = csvread('test.txt')

    x =
    
    23.00000   225.93500   341.77000    17.65800
    24.00000   225.93500   341.75800    17.78200
     0.00000   228.15200   353.93500    17.66500
     0.00000   288.30400   332.87800    24.07400
    
  3. dlmread works in the same way as csvread

    x = dlmread('test.txt')

    x =
    
    23.00000   225.93500   341.77000    17.65800
    24.00000   225.93500   341.75800    17.78200
     0.00000   228.15200   353.93500    17.66500
     0.00000   288.30400   332.87800    24.07400
    
  4. textscan works, results are stored in a cell array

    fid = fopen('test.txt');

    x = textscan(fid,'%s %f %f %f','Delimiter',',')

    x =
    {
      [1,1] =
      {
        [1,1] = 0023
        [2,1] = 0024
        [3,1] = LTAX17
        [4,1] = LTAX24
      }
      [1,2] =
    
        225.94
        225.94
        228.15
        288.30
    
     [1,3] =
    
        341.77
        341.76
        353.94
        332.88
    
     [1,4] =
    
        17.658
        17.782
        17.665
        24.074
    
    }
    >> fclose(fid);
    

I haven't done textread, but you get the idea.

Upvotes: 8

Related Questions