필터 지우기
필터 지우기

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