creating a strucure from a string from my Tektronix MDO3034 oscilloscope

3 Ansichten (letzte 30 Tage)
Hi
I get this string from my oscilloscope
:WFMPRE:BYT_NR 2;BIT_NR 16;ENCDG ASCII;BN_FMT RI;BYT_OR MSB;WFID "Ch1, DC coupling, 50.00mV/div, 20.00us/div, 10000 points, Sample mode";NR_PT 10000;PT_FMT Y;XUNIT "s";XINCR 20.0000E-9;XZERO -80.8000E-6;PT_OFF 0;YUNIT "V";YMULT 7.8125E-6;YOFF -14.7200E+3;YZERO 0.0E+0;VSCALE 50.0000E-3;HSCALE 20.0000E-6;VPOS -2.3000;VOFFSET 0.0E+0;HDELAY 19.2000E-6;DOMAIN TIME;WFMTYPE ANALOG;CENTERFREQUENCY 0.0E+0;SPAN 0.0E+0;REFLEVEL 0.0E+0
and I want to create a structure wfmpre that splits it up like this.
wfmpre.WFMPRE_BYT_NR='2'
wfmpre.BYT_NR='2'
wfmpre.ENCDG='ASCII'
wfmpre.BN_FMT='RI'
wfmpre.BYT_OR='MSB'
wfmpre.WFID='Ch1, DC coupling, 50.00mV/div, 20.00us/div, 10000 points, Sample mode'
You get the idea... But how can I do it?

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Nov. 2015
Bearbeitet: Stephen23 am 24 Nov. 2015
Using textscan makes this task easy, and also allows us to use the '%q' format specifier so that double-quoted strings are kept together (even if they contain delimiter characters).
S = ':WFMPRE:BYT_NR 2;BIT_NR 16;ENCDG ASCII;BN_FMT RI;BYT_OR MSB;WFID "Ch1; DC coupling, 50.00mV/div, 20.00us/div, 10000 points, Sample mode";NR_PT 10000;PT_FMT Y;XUNIT "s";XINCR 20.0000E-9;XZERO -80.8000E-6;PT_OFF 0;YUNIT "V";YMULT 7.8125E-6;YOFF -14.7200E+3;YZERO 0.0E+0;VSCALE 50.0000E-3;HSCALE 20.0000E-6;VPOS -2.3000;VOFFSET 0.0E+0;HDELAY 19.2000E-6;DOMAIN TIME;WFMTYPE ANALOG;CENTERFREQUENCY 0.0E+0;SPAN 0.0E+0;REFLEVEL 0.0E+0';
C = textscan(S,'%q','Delimiter',{';',' '});
C = reshape(C{1},2,[]);
C(1,:) = regexprep(C(1,:),{'^[^A-Z]+','\W'},{'','_'},'ignorecase');
out = struct(C{:})
creates this structure:
out =
WFMPRE_BYT_NR: '2'
BIT_NR: '16'
ENCDG: 'ASCII'
BN_FMT: 'RI'
BYT_OR: 'MSB'
WFID: 'Ch1; DC coupling, 50.00mV/div, 20.00us/div, 10000 points, Sample mode'
NR_PT: '10000'
PT_FMT: 'Y'
XUNIT: 's'
XINCR: '20.0000E-9'
XZERO: '-80.8000E-6'
PT_OFF: '0'
YUNIT: 'V'
YMULT: '7.8125E-6'
YOFF: '-14.7200E+3'
YZERO: '0.0E+0'
VSCALE: '50.0000E-3'
HSCALE: '20.0000E-6'
VPOS: '-2.3000'
VOFFSET: '0.0E+0'
HDELAY: '19.2000E-6'
DOMAIN: 'TIME'
WFMTYPE: 'ANALOG'
CENTERFREQUENCY: '0.0E+0'
SPAN: '0.0E+0'
REFLEVEL: '0.0E+0'
To convert the strings with number values to numeric, then replace the last line with this:
num = cellfun(@str2double,C(2,:));
idx = ~isnan(num)|strcmpi('NaN',C(2,:));
C(2,idx) = num2cell(num(idx));
out = struct(C{:})
which creates this output:
out =
WFMPRE_BYT_NR: 2
BIT_NR: 16
ENCDG: 'ASCII'
BN_FMT: 'RI'
BYT_OR: 'MSB'
... etc
  4 Kommentare
Micke Malmström
Micke Malmström am 25 Nov. 2015
Bearbeitet: Micke Malmström am 25 Nov. 2015
Is this updated code with "textscan" is faster than the previous one with only regexp? Or should they be the same?
Stephen23
Stephen23 am 25 Nov. 2015
I did not test the timing, the advantage of textscan is that it is much more robust for handling double-quoted strings and is also much easier to understand. It is possible that it is faster.

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