Reading character and numeric data

3 visualizzazioni (ultimi 30 giorni)
Mr. CFD
Mr. CFD il 24 Mag 2013
Hi,
I have the following dataset:
'9 b 4 13'
'b4 c0 93 94'
'10 4 8 b'
'8 10 8 d'
'18 17 e 17'
'5 9 4 9'
'11 9 9 16'
I need to specify the first two data entries (seperated by space) in each row, which can be a combination of numeric and characters, with varying size lengths into seperare variables. So for example:
row1=['9';'b']
row2=['b4';'c0']
row3=['10';'4']
I have tried many different avenues with limited success. Can someone please advise?

Risposte (1)

Walter Roberson
Walter Roberson il 24 Mag 2013
sscanf() cannot be used to do this in a single call. The %s format element can be used to a string, and %*s to skip a string; however, if you have multiple strings read in the same call then sscanf() will concatenate them together in the output.
You are better off using
textscan(OneRow, '%s%s%*s%*s')
where OneRow is a row of your input.
Neither textscan nor sscanf can accept cell arrays of strings; if that is what you have then you can do them all at one time by using
regexp(CellArray, '(\S+)\s+(\S+).*', 'match')
This would return a cell array, each entry of which was a cell array per line, with (if possible) the two matched strings, with the rest of each line ignored.
A version simpler to code the pattern for is
regexp(CellArray, '\s+', 'split')
This would also return a cell array, each entry of which was a cell array per line, with as many strings as there were per input line; you would then code to ignore the entries you did not need.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by