eliminate a letter after getting a matrix data from text file

2 visualizzazioni (ultimi 30 giorni)
Questo/a domanda è stato/a segnalato/a da dpb
Hello, I am collecting data from a text file. This data is rearraged accoding to the position of the columns (e.g., # HHE HHN HHZ) by using texline1, textline2 and so son.
textline1 = '# HNE HNN HNZ';
textline2 = '# HNE HNZ HNN';
textline3 = '# HNN HNE HNZ';
textline4 = '# HNN HNZ HNE';
textline5 = '# HNZ HNE HNN';
textline6 = '# HNZ HNN HNE';
I getting different information from the text file and part of one information (estated above) is as follow and collects all the numerical data until the end of the file after "# HHE HHN HHZ" as described in the script below
Part of the text file:
5. ACCELERATION DATA
# HHE HHN HHZ
-0.02104708 -0.02134472 0.00412299
-0.00340606 0.08357343 0.02083563
-0.02940362 0.00093856 0.0050514
. . .
. . .
. . .
Part of the script:
%First mixed data%
if index==0
index = strcmp(tline,textline1); %%EO NS UD
if index ==1; index=1; end
elseif index ==1
tmp=sscanf(tline,'%f %f %f %f');
tmp1 = [tmp(1); tmp(2); tmp(3)]; % rearrange to EO=X NS=Y UD=Y
Output = [Output; tmp1'];
end
This scripts works perfectly capturing the "Ouput" data after "# HHE HHN HHZ". The only problem is that some text files present the following case:
5. ACCELERATION DATA
# HHE HHN HHZ
T
-0.02104708 -0.02134472 0.00412299
-0.00340606 0.08357343 0.02083563
-0.02940362 0.00093856 0.0050514
A "T" letter ruins all the collection of the numerical data after "# HHE HHN HHZ". There is a way in which I can only add and aditional "script line" after "tmp=sscanf(tline,'%f %f %f %f');" in the previous script which eliminates the letter "T" and capture the data "tmp1" to rearrange it according to the textline (e.g., textline1) withouth making too many changes in the script. So, for the case in which a letter "T" appears after "# HHE HHN HHZ" just get rid of it and only capture the numerical values.
Thank you for your response

Risposta accettata

dpb
dpb il 8 Mag 2023
Modificato: dpb il 10 Mag 2023
I've given you the Answer for both of these earlier at the <Original Q? Thread> location and the followup discussion.
Will close this shortly as duplicate..
But, after the previous comment/sample code to return the sampled channels, recast the previous code similarly.
Remember, the Q? you asked before was to only read that one data array; you didn't provide the asked-for context to build a more generic solution..."help us help you!".
function acc=getAccel(fid,chnlstr) % presume file already open, pass handle, channel string identified in the previous function
% find acceleration section of form
% # 5. ACCELERATION DATA
% # HNE HNN HNZ
% T
0.02165885 -0.06615625 0.00254670
0.13723481 -0.08534159 -0.00400487
...
% return acceleration data given matching channel string to look for
MATCHSTR=chnlstr;
l=fgetl(fid);
while ~startsWith(l,MATCHSTR)
l=fgetl(fid);
end
acc=fscanf(fid,'%f',[3,inf]).'; % try to convert assuming data (no "T" record)
if isempty(acc) % if it fails, there must have been the T record, skip it
fgetl(fid);
acc=fscanf(fid,'%f',[3,inf]).'; % now read the accel data
end
end
CAUTION: Semi-Air code!!! but tested with a locally-created text file that contained the "T" and some accel data following.
The sprintf will fail and leave the file pointer unmoved if it doesn't succeed; thus then the fgetl will pull that record and throw it into the bit bucket and leave positioned to read the subsequent data.
NOTA BENE: This routine does NOT close the file handle; that's left for the top-level routines to do; it also does not sort the output array; the sorting index will have just been retrieved prior to this function being called and the top level code can do the rearrangement. Or, you can choose to pass the indexing array as well and do the sort internally, your call on factoring the code.
The above also doesn't check for errors; if used properly in the right sequence, it shouldn't fail to match the input channel string, but it would never hurt to check for feof() or add a "bail out" counter in order to handle any such events cleanly.
Again, I'd emphasize to break the task down to the small individual pieces and then put those together at the higher level.
  5 Commenti
dpb
dpb il 8 Mag 2023
Modificato: dpb il 9 Mag 2023
Glad to help...remember Einsteins admonishment to simplify...same thing here; think hard about the code first and break it down into individual, manageable pieces that can string together instead of just trying to put something together all in one piece...the couple sample routines should be a good starting point for how to factorize.
dpb
dpb il 9 Mag 2023
ERRATUM:
"NB: I forgot to put the second fscanf in an else clause to avoid trying to read after a successful first pass..."
That's a mistake, too...it doesn't belong in an else clause, it belongs inside the if construct to read the data after tossing the "T" record away.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by