How to read a file in matlab?

I have a txt file, and the contents of the file are strings of numbers, each line has 5 floating point numbers, with each comma separated by a comma. Example:

1.1, 12, 1.42562, 3.5, 2.2

2.1, 3.3, 3, 3.333, 6.75

How can I read the contents of a file in a matrix in matlab? So far I have this:

fid = fopen('file.txt'); comma = char(','); A = fscanf(fid, ['%f', comma]); fclose(fid); 

The problem is that it gives me only the first line and when I try to write the contents of A, I get this: 1.0e + 004 * some number

Can someone help me? I think for the file I need to read it in a loop, but I don't know how to do it.

Edit: Another question: when I conclude in A, I get the following:

 A = 1.0e+004 * 4.8631 0 0 0 0.0001 4.8638 -0.0000 -0.0000 0.0004 0.0114 4.8647 -0.0000 -0.0000 0.0008 0.0109 

I want the same values ​​as in the file to be in the matrix, how can I make the numbers regular floats and not format them like that? Or are the numbers in the matrix actually floating, but the output is only displayed like that?

+4
source share
3 answers

The MATLAB dlmread function will be much simpler for what you want to execute.

 A = dlmread('filename.txt',',') % call dlmread and specify a comma as the delimiter 
+8
source

try using the importdata function

 A = importdata(`filename.txt`); 

He will solve your question.

EDIT

Alternative 1)

 A = dlmread('test_so.txt',','); 
+6
source

The answer is surprisingly simple:

 fid = fopen('depthMap.txt'); A = fscanf(fid, '%f'); fclose(fid); 
0
source

All Articles