Can someone help me to read the data in the attached .txt file using fscanf? Edit: Also how can I add a space to a string in a certain location?

2 Ansichten (letzte 30 Tage)
The attached file has been written using fprintf. By the way, why does the text appear aligned when it's opened with notepad and not aligned when opened with matlab?
The function used to write the text file is the following:
function [] = IPC2(namefile, d, epcheck)
%This Function registers input data to a text file for further use.
global ha hp i omega OMEGA teta0
if epcheck == 3
psp = evalin('base', 'psp');
else
psp = ' ';
end
alphadate = evalin('base', 'alphadate');
fID = fopen([namefile,'.txt'],'w');
fprintf(fID,'%s \r\n', 'UNINA SPACE MAST Input File');
fprintf(fID,'%s %i', 'Input Type Identifier:', 1);
fprintf(fID,'\r\n \r\n');
header = strings(9);
v = nan(8);
u = strings(6);
header(1) = 'ha:';
header(2) = 'hp:';
header(3) = 'i:';
header(4) = 'omega:';
header(5) = 'OMEGA:';
header(6) = 'teta0:';
header(7) = 'Date & time:';
header(8) = 'alphadate:';
header(9) = 'Satellite''s entry point:';
v(1) = ha/1000;
v(2) = hp/1000;
v(3) = i*180/pi;
v(4) = omega*180/pi;
v(5) = OMEGA*180/pi;
v(6) = teta0;
v(7) = psp;
v(8) = alphadate*180/pi;
u(1) = 'km';
u(2) = 'km';
u(3) = '°';
u(4) = u(3);
u(5) = u(3);
u(6) = u(3);
u(7) = '%';
u(8) = u(3);
for ii = 1 : 6
fprintf(fID, '%s \t\t', header(ii));
fprintf(fID, '%f ', v(ii));
fprintf(fID, '%s\r\n', u(ii));
end
fprintf(fID, '\r\n');
fprintf(fID, '%s\r\n', 'GROUND TRACK INPUT');
fprintf(fID, '%s \t\t\t\t', header(7)); fprintf(fID, '%s \r\n', d);
fprintf(fID, '%s \t\t\t\t', header(8)); fprintf(fID, '%s', num2str(alphadate*180/pi));
fprintf(fID, '%s\r\n', u(8));
if epcheck == 1
ep = 'Ascending node';
end
if epcheck == 2
ep = 'Descending node';
end
if epcheck == 3
psp = num2str(psp);
ep = psp;
end
fprintf(fID, '%s \t\t', header(9)); fprintf(fID, '%s', ep);
if epcheck == 3
fprintf(fID, '%s', u(7));
end
end
In this case, epcheck == 3.
I need to get all the numbers in the text file and the datetime string

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Jan. 2019
Bearbeitet: Walter Roberson am 19 Jan. 2019
Preferences -> Editor/Debugger -> Tab . Set the Tab Size to 8.
8 is the traditional tab size, but it is only a convention. If you are not using a typewritter or similar device that has literal physical tab stops, and you are not using the data as input to a program such as WordPerfect or a forms editor that recognizes tab as having very specific meaning different than equivalent spaces, then you should probably not use tab.
With respect to reading the file: Except in the case that you are reading from a serial device and have programmed TAB as the end of line character, fscanf() of a serial device or a file will treat any mixture of tabs and spaces as being a single whitespace. You cannot, for example, use fscanf() with a '%s%s%f' format expecting that 'helloTABTAB3' would be read as 'hello' [] 3 .
If you need tabs to mark the end of fields with two tabs in a row marking a skipped field, then you should process the individual characters yourself, or you should use textscan() as you can call textscan with a Whitespace parameter that does not include tab and Delimiter that includes tab, and you can make sure that MultipleDelimetersAsOne is not true (default is not true.)
  3 Kommentare
Walter Roberson
Walter Roberson am 20 Jan. 2019
Bearbeitet: Walter Roberson am 21 Jan. 2019
namefile = 'prova.txt';
S = fileread(namefile);
Sclean = regexprep(S, {'\s*(°|%|km)\s*$', '(\s*&\s*)|''', '(?<=^[^:]+?) (?=\D)' }, {'', '', ''}, 'lineanchors', 'dotexceptnewline');
Stok = regexp(Sclean, '^(?<id>\S+):\s*(?<value>[^\r\n]+)', 'names', 'lineanchors');
valnumeric = str2double({Stok.value});
valcell = num2cell(valnumeric);
notnum_mask = isnan(valnumeric);
dt_entries = datetime({Stok(notnum_mask).value});
valcell(notnum_mask) = num2cell(dt_entries);
V = cell2struct(valcell, {Stok.id}, 2);
V.ha = V.ha * 1000;
V.hp = V.hp * 1000;
V.i = V.i * pi/180;
V.omega = V.omega * pi/180;
V.OMEGA = V.omega * pi/180;
V.teta0 = V.teta0 * pi/180;
V.alphadate = V.alphadate * pi/180;
V.Satellitesentrypoint = V.Satellitesentrypoint * pi/180;
The output, V, is a struct with one field for each input variable.
The code does not care about the order of the lines. However, it will only attempt to extract values from lines that contain a colon.
The most difficult part of the code was removing the <space>&<space> on the same line as the datetime -- spaces before the first colon had to be removed but not spaces after the first colon.
Also, the code will fail if there are non-numeric entries that are not datetime strings, or if the datetime is not in one of the expected formats.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by