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

1 view (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 27 May 2021
Edited: Stephen23 on 27 May 2021
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!