Using fscanf to make a matrix from text file?

8 vues (au cours des 30 derniers jours)
James
James le 24 Avr 2018
Modifié(e) : Stephen23 le 24 Avr 2018
I have a text file with 5 rows and 4 columns. I would like to turn this data into a matrix, each column having a different conversion character. The following is the data in the file;
c1 c2 c3 c4
4 7 8 345436
7.1 8 56786.27 3.1
2 23456.34521 19.8 6
797.1349 2 4 334048
Here are some examples of what I have tried;
if true
% code
file_name = fopen('HW4Problem2.txt', 'r');
stuff = fgets(file_name);
B = fscanf(file_name, '%i %.2f %i %.3e \n', [4 Inf])
fclose(file_name);
end
This outputs only B = 4.
I've tried this;
if true
% code
file_name = fopen('HW4Problem2.txt', 'r');
stuff = fgets(file_name);
B = fscanf(file_name, '%f \n', [4 Inf])
fclose(file_name);
end
This outputs the correct matrix, except it is giving by rounded decimals multiplied my an exponential. Here's a screenshot of the output.
If instead i try to read them as integers, its outputs this;
I would appreciate any help you can give me, thank you.
  1 commentaire
Stephen23
Stephen23 le 24 Avr 2018
Modifié(e) : Stephen23 le 24 Avr 2018

Note that file_name is a very misleading name for that variable: the output of fopen is not a name, but is a kind of handle or ID to the opened file. Most MATLAB users call that variable fid.

Connectez-vous pour commenter.

Réponses (1)

Stephen23
Stephen23 le 24 Avr 2018
Modifié(e) : Stephen23 le 24 Avr 2018
"This outputs the correct matrix, except it is giving by rounded decimals multiplied my an exponential. "
Your data has been correctly imported to the full precision of the double data type (about 16 decimal sigfig). The default format, which controls how data is displayed in the command window, uses "Short, fixed-decimal format with 4 digits after the decimal point.". You can simply change the format to change how the data is displayed:
>> format longG
>> B
B =
4 7.1 2 797.1349
7 8 23456.34521 2
8 56786.27 19.8 4
345436 3.1 6 334048
>> format longE
>> B
B =
4.00000000000000E+000 7.10000000000000E+000 2.00000000000000E+000 7.97134900000000E+002
7.00000000000000E+000 8.00000000000000E+000 2.34563452100000E+004 2.00000000000000E+000
8.00000000000000E+000 5.67862700000000E+004 1.98000000000000E+001 4.00000000000000E+000
3.45436000000000E+005 3.10000000000000E+000 6.00000000000000E+000 3.34048000000000E+005
Note that in all cases the data stored in MATLAB's memory is exactly the same and does not change.
PS: I used this code:
fid = fopen('HW4Problem2.txt', 'rt');
fgets(fid);
B = fscanf(fid, '%f', [4,Inf])
fclose(fid);
Putting the newline into the pattern is a red-herring, and serves no purpose.
  2 commentaires
James
James le 24 Avr 2018
Thank you this is really helpful. So if I wanted to change the format of an individual column, would i have to separate it into a column vector, and then change the format of that vector?
Stephen23
Stephen23 le 24 Avr 2018
Modifié(e) : Stephen23 le 24 Avr 2018

"So if I wanted to change the format of an individual column, would i have to separate it into a column vector, and then change the format of that vector?"

No, because you cannot "change the format of that vector": numeric vectors do not contain any formatting information whatsoever, so you cannot change what does not exist. Formatting is a feature of the command window, and solely affects how data is displayed. If you want to display particular data with a particular format, then change the format: any data displayed after that will use that format, until you change it again.

Changing the format is not the only way to control how data looks like: if you want more control then you will need to write your own displaying routine using fprintf.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by