Extracting data from text file after a specified word
17 views (last 30 days)
Show older comments
Dear Community,
I need some help with reading a text file, and could not find another answer on this forum.
I have a set of text files that contains x and z data. The files are saved in the same working directory as my script in .txt files. The files do not all have the same structure. The only thing that is constant is that the data starts after the same word. I have attached a .txt file that resembles what my data looks like
My question is. How can I write a script that extract the 3 columns of data underneath the word "Start Data?" I was thinking about writing a script that searches the string "Start Data" and then extracts the lines with numbers underneath. But I do not know how this can be made.
I hope that someone here can help me.
0 Comments
Accepted Answer
Star Strider
on 18 Jan 2024
Edited: Star Strider
on 18 Jan 2024
The textscan function works well for these sorts of problems if the files have a consistent internal structure —
contents = fileread('ExampleTXTFile.txt') % View File Contenst
fidi = fopen('ExampleTXTFile.txt','rt');
k1 = 1;
while ~feof(fidi)
C = textscan(fidi, '%f%f%f', 'HeaderLines',5, 'CollectOutput',true);
M = cell2mat(C);
if isempty(M) % Empty Matrix Indicates End-Of-File
break
end
D{k1,:} = M;
fseek(fidi, 0, 0);
k1 = k1 + 1
end
fclose(fidi);
D % Individual File Sections
Out = cell2mat(D) % Vertically Coincatenated Results
figure
hold on
for k = 1:numel(D)
plot3(D{k}(:,1), D{k}(:,1), D{k}(:,1), '.-', 'DisplayName',["D\{"+k+"\}"])
end
grid on
xlabel('D\{:\}(:,1)')
ylabel('D\{:\}(:,2)')
zlabel('D\{:\}(:,3)')
legend('Location','best')
view(-30,30)
EDIT — (18 Jan 2024 at 15:26)
Added plot.
.
6 Comments
More Answers (1)
Hassaan
on 18 Jan 2024
data = extractDataFromTextFile('Text.txt');
disp(data)
function data = extractDataFromTextFile(filename)
% Open the file for reading
fid = fopen(filename, 'rt');
if fid == -1
error('Cannot open file: %s', filename);
end
startData = false; % Flag to indicate when to start reading data
data = []; % Initialize an empty array to store the data
% Read the file line by line
while ~feof(fid)
line = fgetl(fid);
% Check if the line contains 'Start Data'
if contains(line, 'START DATA')
startData = true;
continue;
end
% Extract data if startData flag is true
if startData
numData = str2num(line);
% Check if the line has exactly three numeric values
if length(numData) == 3
data = [data; numData];
elseif ~isempty(numData)
% Stop reading if a line doesn't have three numbers
break;
end
end
end
% Close the file
fclose(fid);
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
0 Comments
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!