HOW TO IMPORT SPECIFIC DATA FROM A TEXT FILE

1 Ansicht (letzte 30 Tage)
Alberto Marchi
Alberto Marchi am 24 Nov. 2016
Kommentiert: Shivani thaduru am 27 Dez. 2018
Hi, I have a text file like this:
*HEADER
....
*PRELOAD
1 12
2 45
3 65
4 ...
*VALUES
1 85
2 96
3 ...
and I want to create arrays such as:
preload = [12 45 65 ...] , values = [85 96 ...],... .
I tried many functions such as dlmread, fscanf, importdata etc. but I couldn't find a way to make matlab find the titles ('*HEADER', '*...') and store the values after them. Could anyone help me? Thanks

Akzeptierte Antwort

KSSV
KSSV am 25 Nov. 2016
fid = fopen('your txt file','r') ;
S = textscan(fid,'%s','Delimiter','\n');
S = S{1} ;
fclose(fid) ;
%%Get the line number of PRELOAD and VALUES
idxS1 = strfind(S, '*PRELOAD');
idx1 = find(not(cellfun('isempty', idxS1)));
idxS2 = strfind(S, '*VALUES');
idx2 = find(not(cellfun('isempty', idxS2)));
% get the required
preload = cell2mat(cellfun(@str2num,S(idx1+1:idx2-1),'un',0)) ;
values = cell2mat(cellfun(@str2num,S(idx2+1:end),'un',0)) ;
  4 Kommentare
KSSV
KSSV am 25 Nov. 2016
Yes it can be...
Shivani thaduru
Shivani thaduru am 27 Dez. 2018
Hi kssv,
How do I give inputs in text scan if I want to read text file with each line of below format?
'[09:16.046] LTE Path 47 Band 2 Not CA Frequency 1901.8 Expected Power 21.0 50RB_Full RB Start Pos 0 BW 10.0 MHz Mod 64QAM Tx Power 999999.00 Limits 20 to 22 [dBm] Failed'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by