read a text file and print float point number
Show older comments
I have atext file that want to read and print the 5th and 7th columns. I did as follow
fidi = fopen(['/Users/MATLAB/Proton-deflection_via_Bfield/Proton_range_water_SRIM.txt'],'rt'); % read srim data
Dc = textscan(fidi, '%f%s %f %f %f %s %u%s %u %s', 'HeaderLines',HL, 'CollectOutput',true, 'EndOfLine','\r\n');
fclose(fidi);
The problem is my code doesn't show numbers after decimal point for the 5th and 7th columns: for instance for 10 MeV protons the range is 1.2 mm in the text but my code prints 1mm and not 0.2 um. I used different formats for reading it but doesn't solve the problem. When I use %f instead of %u code shows the 6th colum instead which is not acceptable. I appreciate any suggestion.
2625
2826
3021
3211
33952
6889
7209
7836
8447
9047
9640
1
1
1
1
1
2
2
.
.
.
.
487
560
638
808
996
1
2 Comments
Mathieu NOE
on 26 Oct 2020
hello
I ran your code (with HL = 24) and changed the %u to %f to get the decimals
Dc = textscan(fidi, '%f%s %f %f %f %s %f %s %f %s', 'HeaderLines',HL, 'CollectOutput',true, 'EndOfLine','\r\n');
fclose(fidi);
% test
tmp = Dc{3};
tmp(end,:) % gives : 4.6570 0.0027 1.2000
% test
tmp = Dc{5};
tmp(end,:) % gives : 53.8800
% test
tmp = Dc{7};
tmp(end,:) % gives : 34.0500
Masoud Afshari
on 26 Oct 2020
Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!