Extract column from text file
1 次查看(过去 30 天)
显示 更早的评论
Hi, for my research project I have to analyze a certain output. The output gives a lot of measurements (also a lot of data I don't need), but I don't know how to extract the specific column of data that I do need. The data looks like this:
P1,R0,14:17:36.779,47,0.01421266,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0,0,0,-5.2,0
P0,R0,14:17:36.823,47,0.01403595,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0.08738,0.6293842,17.97661,-5.2,0
But I only need the first number between brackets (value -0.03077913 here). How do I do this?
2 个评论
the cyclist
2023-6-12
The best method will depend on exactly what format of file you have. (For example, is it Excel, a CSV, etc.?) Can you upload the file (using the paper clip icon from the INSERT area of the toolbar), or at least a few representative lines?
回答(2 个)
Kautuk Raj
2023-6-13
To extract the first number between the brackets in the data provided, we can use regular expressions in MATLAB.
This regular expression pattern \[([-0-9\.]+);([-0-9\.]+);([-0-9\.]+)\] will match the three numbers between the brackets and captures them in separate groups.
0 个评论
Stephen23
2023-6-13
Fake data (you would use FILEREAD):
S = sprintf('%s\n','P1,R0,14:17:36.779,47,0.01421266,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0,0,0,-5.2,0','P0,R0,14:17:36.823,47,0.01403595,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0.08738,0.6293842,17.97661,-5.2,0')
C = regexp(S,'(?<=\[)(-|+)?\d+\.?\d*','match')
V = str2double(C)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!