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?
0 件のコメント
採用された回答
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 件のコメント
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!