creating a strucure from a string from my Tektronix MDO3034 oscilloscope
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Micke Malmström
el 24 de Nov. de 2015
Comentada: Stephen23
el 25 de Nov. de 2015
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 comentarios
Respuesta aceptada
Stephen23
el 24 de Nov. de 2015
Editada: Stephen23
el 24 de Nov. de 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 comentarios
Stephen23
el 25 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!