How to import data of the form [variable name]Data[-]
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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.DataPoints
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!