How to import data of the form [variable name]Data[-]

조회 수: 2 (최근 30일)
Simon Dengler
Simon Dengler 2022년 2월 12일
댓글: Simon Dengler 2022년 2월 14일
I want to read data and variables from a specially formatted file as MATLAB variables. Preferably into a structure.
I am looking for the best approach for a quick solution. unfortunately I am stuck. Maybe someone can give me a helpful hint.
Data in the file have the strucktur:
[variable name]
data
[-]
[variable name]
x1 y1
x2 y2
x3 y3
[-]
A shortened example file:
Instrument Name
File Format Version
[DateTime]
1643810991
[-]
[GenParams]
[CableID]
[-]
[FiberID]
[-]
[FiberType]
0
[-]
[Settings]
[Wavelength]
1
[-]
[PulseWidth]
7
[-]
[End]
35.000
[-]
[Start]
0.000
[-]
[DataPoints]
0.011 -5.000
0.011 -5.000
0.036 -5.000
0.061 -5.000
[-]
  댓글 수: 6
Simon Dengler
Simon Dengler 2022년 2월 14일
Of course, I've already looked at the manufacturer, who doesn't provide anything. the company is also far too small and the product far too niche. I will have to tackle the task myself.
I haven't built a praser yet. now I'm looking for information how to do that
Stephen23
Stephen23 2022년 2월 14일
@Simon Dengler: please upload a sample file by clicking the paperclip button.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 2월 14일
편집: Stephen23 2022년 2월 14일
rgx = '\[(\w+)\]\s*([^\[]+)\[\-\]';
str = fileread('test.txt');
tkn = regexp(str,rgx,'tokens');
tkn = vertcat(tkn{:});
tmp = cellfun(@str2num,tkn(:,2),'uni',0);
out = cell2struct(tmp,tkn(:,1),1);
out.DateTime = datetime(out.DateTime,'ConvertFrom','posix')
out = struct with fields:
DateTime: 02-Feb-2022 14:09:51 CableID: [] FiberID: [] FiberType: 0 Wavelength: 1 PulseWidth: 7 End: 35 Start: 0 DataPoints: [4×2 double]
out.DataPoints
ans = 4×2
0.0110 -5.0000 0.0110 -5.0000 0.0360 -5.0000 0.0610 -5.0000
  댓글 수: 1
Simon Dengler
Simon Dengler 2022년 2월 14일
Thank you very much!! That's perfect, and so wonderfully simple.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by