Write arrays with exact values from .txt files with scientific notation

Hello together,
i read values from .txt files. The values are in scientific notation and have the form
0.7553396358543418E-01
0.7553483893557424E-01
.....
My suggestion for this:
folder = ['foldername'];
filename_t = fullfile(folder, sprintf('%dHz_t_LS_%d.txt', f_sim, LS));
fileID_t = fopen(filename_t, 'rt');
t_total = textscan(fileID_t, '%sf,'HeaderLines',0);
t_total = t_total{1};
But unfortunately it never gives the correct value. I only get values in the form 0.0750, which is much too low accuracy.
i have already experimented with %16.8f etc., but did not reach my goal
Many greetings

2 Comments

"My suggestion for this"
will not work because the syntax is not valid:
t_total = textscan(fileID_t, '%sf,'HeaderLines',0);
% ^ ^ ^
this was a copy error, so the correct command would be
t_total = textscan(fileID_t, '%f','HeaderLines',0);
but even this does not give the desired result

Sign in to comment.

 Accepted Answer

I suspect you have the format set at:
format short
Setting it to
format long
may be the solution,.

9 Comments

unfortunately this is not the solution to the problem
This could be the answer.
How do you access the value you wrote 0.0750?
teasy — Perhaps we could do more to help if we had the text file to work with.
Here I have attached the file. The problem I see is that Matlab only takes 4 decimal places
Try this function, supply it path to your file
Input_File_Path = 'C:\folders\54400Hz_t_LS_13.txt'
function File_Data = Read_File(Input_File_Path)
% Read_File reads supplied input file path
FID = fopen(Input_File_Path, 'r');
Counter = 1;
tline = fgetl(FID);
File_Data{Counter} = tline;
while ischar(tline)
Counter = Counter+1;
tline = fgetl(FID);
File_Data{Counter} = tline;
end
File_Data(end) = []; % removes end of file line
File_Data = File_Data'; % moves data into column
File_Data = cellfun(@str2double, File_Data);
fclose(FID);
Output
>> format short
>> File_Data(1)
ans =
0.0752
>> format long
>> File_Data(1)
ans =
0.075221463585434
Mario Malic — Thank you.
teasy — this is what I get with your code and file:
format long
fileID_t = fopen('54400Hz_t_LS_13.txt','rt');
t_total = textscan(fileID_t, '%f','HeaderLines',0);
fclose(fileID_t);
t_totalm = t_total{1}
producing (for the first 5 values):
t_totalm =
0.075221463585434
0.075222338935574
0.075223258053221
0.075224177170868
0.075225096288515
The rest are there as well, I simply did not reproduce them here.
Thanks a lot!
Individually the code of Mario Malic works.
But in the context of how I use it, it does not work.
It must have something to do with the Format command, because I still only get 4 decimal places in my code
Well, That's why the context is important in the question.
The problem was that I looked at the variable in the workspace and the workspace display was short.
Sorry for the circumstances but thanks for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 12 Sep 2020

Commented:

on 13 Sep 2020

Community Treasure Hunt

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

Start Hunting!