1

I have two .dat files. They are world.dat and sensor_data.dat. I have a folder name in D: drive named tutorial. Within this tutorial file, there are two folders data and code. Now in the data folder, there are two files as I mentioned earlier world.dat and sensor_data.dat. In the code folder, there is a file name main.m as it is a Matlab file.

The code that is written on this file(main.m) is

clc;
clear;
close all;

% Read *.dat files containing landmark data
landmarks = fopen('../data/world.dat');
landmarks_data = fread(landmarks);

% Read *.dat files containing odometry and range-bearing sensor data
data = fopen('../data/sensor_data.dat');
data_data = fread(data);

But when I print landmarks_data and data_data they print something other than that is written on those two files(world.dat,sensor_data.dat)

world.dat file contains:

1 2 1
2 0 4
3 2 7
4 9 2
5 10 5
6 9 8
7 5 5
8 5 3
9 5 9

My output:

>> landmarks_data
landmarks_data =
49
32
50
32
49
10
50
32
48
32
52
10
51
32
50
32
55
10
52
32
57
32
50
10
53
32
49
48
32
53
10
54
32
57
32
56
10
55
32
53
32
53
10
56
32
53
32
51
10
57
32
53
32

I don't know where they get those data? The same thing happened for data_data variable.

Need help to fix the problem.

Encipher
  • 1,370
  • 1
  • 14
  • 31

1 Answers1

2

You are getting the ASCII values of the characters in the file.

ASCII value of 1 equals 49.
ASCII value of ' ' (space) equals 32.
ASCII value of 2 equals 50...

fread reads data from binary file, and you are using fread for reading a text file. The binary value of a text character is the ASCII code (it can also be a UNICODE value).

In case you want to read the data as text, and keep the matrix structure, you can use readmatrix function:

landmarks = readmatrix('../data/world.dat');

Result:

landmarks =

     1     2     1
     2     0     4
     3     2     7
     4     9     2
     5    10     5
     6     9     8
     7     5     5
     8     5     3
     9     5     9

Remark: In case your MATLAB version is before R2019a, you can use dlmread instead.

Rotem
  • 30,366
  • 4
  • 32
  • 65