How can I export only specific data from a text file to a matrix?

I'm need to import only the numeric data of the attached .txt file into a 9x8 matrix and I'm not sure how to do this. Any help is greatly appreciated!

3 Kommentare

Adam Danz
Adam Danz am 24 Jul. 2019
Bearbeitet: Adam Danz am 24 Jul. 2019
I see several sections of numeric data, one of which is a 9x8 matrix. Where does the 2x2 come from?
Also do all of the text files follow this exact same format? For example is ":Solar_Radio_Flux" always on the 8th line?
What Adam said...
But, it's generally simpler to read what section of a file contains the info of interest and then just throw out what you don't need than it is to specifically hunt and peck through a file reading a bit here and there.
But, it certainly isn't at all clear what you want to retrieve from this file given the question wording...
My bad! I'm not sure where I got 2x2 from either lol, it was late in the day when I posted. I'm looking to grab that 9X8 matrix and import into MATLAB. And yes Adam, I believe the text files follow this exact same format.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 25 Jul. 2019
Bearbeitet: Jan am 26 Jul. 2019
With some guessing, that you want the part behind ':Solar_Radio_Flux:' :
Key = ':Solar_Radio_Flux:';
Str = fileread(FileName);
C = strsplit(Str, newline);
index = find(strncmp(C, Key, numel(Key)));
Block = C(index+3:index+11);
BlockStr = sprintf('%s ', Block{:}); % Horizontal concatenation
% Data = sscanf(BlockStr, '%g ', [9, 8]); [EDITED:]
Data = sscanf(BlockStr, '%g ', [8, 9])'; % Thanks Adam
This can be expanded e.g. to read the 2 header lines also or to accept any block of numbers until the next comment line.

2 Kommentare

Thanks so much! Time for me to try and figure out what this means:)
That last line should be
Data = sscanf(BlockStr, '%g ', [8, 9])';

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

rows = 11:19; % NUMERIC ROWS IN TEXT FILE TO IMPORT
nColumns = 8; % NUMBER OF COLUMNS OF TEXT
file = 'Testdata.txt'; % THE FULL PATH WOULD BE BETTER
opts = delimitedTextImportOptions('NumVariables',nColumns,'DataLines',rows([1,end]),'Delimiter', " ", ...
'VariableTypes',repmat({'double'},1,nColumns),'ConsecutiveDelimitersRule','join',...
'LeadingDelimitersRule','ignore');
Testdata = readtable(file, opts);
Testdata = table2array(Testdata);
Result
Testdata =
245 14 12 11 -1 -1 -1 -1
410 25 28 26 -1 -1 -1 -1
610 34 -1 34 -1 -1 -1 -1
1415 45 49 55 -1 -1 -1 -1
2695 104 -1 68 -1 -1 -1 -1
2800 -1 -1 -1 68 68 -1 -1
4995 107 113 126 -1 -1 -1 -1
8800 223 230 218 -1 -1 -1 -1
15400 519 549 496 -1 -1 -1 -1

5 Kommentare

Thanks for this, this was the easiest to understand!
No problem! The main difference between this solution and Jan's is that this one only reads in the matrix data while Jan's reads in the whole file and then hones in on the matrix data.
Last line is simply Testdata{:,:}
Adam Danz
Adam Danz am 25 Jul. 2019
Bearbeitet: Adam Danz am 25 Jul. 2019
@madhan ravi, it's the same. That's the only line in the table2array function. Matlab's import data tool uses table2array() which is why I ended up using it, too. But I agree that Testdata{:,:} is more direct.
Or if you have R2019a
Testdata = readtable(file, opts);
Testdata = table2array(Testdata);
can just be
A = readmatrix(file,opts)

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 24 Jul. 2019

Bearbeitet:

Jan
am 26 Jul. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by