Reading Text file and converting values to variables (*not in matrix form*)

1 次查看(过去 30 天)
Hello,
I have noticed there are my solutions to creating variables from text files that are in a matrix layout with rows and collumns, however, I am recieving text files from an external source that is in a paragraph-type form. I would like to create varaibles and assign the values in the text file to them...
There might be an issue with where the text file is located - it is in a different folder than what the rest of my script is running. Might need to open folder and paste the path to the text file. Either way, if someone could help me pull the variables from the text, I would much appreciate it!
I would like these variables from the text:
timestamps =
top_left_x =
top_left_y =
bottom_right_x =
bottom_right_y =
maximum =
--------------------------------------
Here is the sample of the text file:
14 EXPORTS
Timestamps [seconds] - 0,1,...,15:
2.1,2.2,2.7,2.73,2.82,2.89,2.94,2.99,3.11,3.23,3.34,3.39,3.40,3.47,3.52
Top left [distance] - x, y:
-0.0963987, -0.00506739
Bottom right [distance] - x, y:
0.0963985, 0.224873
Maximum [rate]:
2

采纳的回答

Stephen23
Stephen23 2021-5-27
编辑:Stephen23 2021-5-27
P = 'absolute or relative path to where the file is saved';
S = fileread(fullfile(P,'output.txt'));
F = @(t)sscanf(regexprep(S,sprintf('^.+%s[^:]*:',t),''),'%f,');
TS = F('Timestamp')
TS = 15×1
2.1 2.2 2.7 2.73 2.82 2.89 2.94 2.99 3.11 3.23
TL = F('Top left')
TL = 2×1
-0.0963987 -0.00506739
BR = F('Bottom right')
BR = 2×1
0.0963985 0.224873
MX = F('Maximum')
MX =
2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by