creating a strucure from a string from my Tektronix MDO3034 oscilloscope

1 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2015-11-24
编辑:Stephen23 2015-11-24
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 个评论
Micke Malmström
Micke Malmström 2015-11-25
编辑:Micke Malmström 2015-11-25
Is this updated code with "textscan" is faster than the previous one with only regexp? Or should they be the same?
Stephen23
Stephen23 2015-11-25
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by