HOW TO IMPORT SPECIFIC DATA FROM A TEXT FILE

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

1 Stimme

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

Alberto Marchi
Alberto Marchi am 25 Nov. 2016
Thank you so much!! perfectly working!
Alberto Marchi
Alberto Marchi am 25 Nov. 2016
Sorry to bother you again, I encoutered a tab like this:
*SIMULATIONS
1 VEL1 PRE1 POS1
2 VEL1 PRE1 POS2
3 VEL1 PRE1 POS3
4 VEL2 PRE2 POS1
5 VEL2 PRE2 POS3
6 VEL3 PRE4 POS7 ....
In this case, is it possible to read these lines and associate (for example) to 'PRE1' the first value that I encountered in tab '*PRELOAD' and the same for velocity and position...and finally recreate the matrix:
VEL1 PRE1 POS1
VEL1 PRE1 POS2
...
KSSV
KSSV am 25 Nov. 2016
Yes it can be...
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