HOW TO IMPORT SPECIFIC DATA FROM A TEXT FILE
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
KSSV
on 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 Comments
Shivani thaduru
on 27 Dec 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'
More Answers (0)
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!