Graphing data from a .txt file giving error and inaccurate data

2 views (last 30 days)
Hello, I am having some issues with graphing data from a .txt file. The data is attached and I am trying to graph time vs tempature but it is giving an error as seen in the photo and it is getting inaccuate data from the file. This code I've been trying worked on a less complex file. I also need to graph time vs pressure and time vs alitude but I assume it is the same method once it is figured out. Does anyone have any suggestions? Thank you in advance
clear all,clc
%Time
fid = fopen('RTSDATA.txt','r');
c = textscan(fid,'%s','Delimiter',{'\n',' '},'MultipleDelimsAsOne',1);
fclose(fid);
TimeInd = find(startsWith(c{1},'Elapsed')) + 1;
Time(size(TimeInd,1)) = 0;
for i = 1:size(TimeInd,1)
Time(i) = sscanf(c{1}{TimeInd(i)}, '%f');
end
%Temperature
fid = fopen('RTSDATA.txt','r');
c = textscan(fid,'%s','Delimiter',{'\n',' '},'MultipleDelimsAsOne',1);
fclose(fid);
TempInd = find(startsWith(c{1},'Temperature')) + 2;
Temp(size(TempInd,1)) = 0;
for i = 1:size(TempInd,1)
Temp(i) = sscanf(c{1}{TempInd(i)}, '%f');
end
figure(1)
plot(Time(1:end-1), Temp)
title("Time vs Temperature")
xlabel('Time')
ylabel('Temperature(*C)')

Accepted Answer

Rik
Rik on 11 May 2021
You should probably read the file once and then parse the data. I cropped your file, because I don't think the entire thing is required to show the method.
The example below uses the readlines function introduced in R2020b. If you need to use this on older releases, you can get my readfile function from the FEX. If you are using R2017a or later, you can also get it through the AddOn-manager.
data=readlines('RTSDATA.txt');
Time=data(contains(data,'Time Elapsed')); %select the relevant lines
Time=regexprep(Time,'[^,]*,([^,]*),[^,]*','$1'); %keep only the text between the commas
Time=str2double(Time); %convert the text to values
Temp=data(contains(data,'Temperature')); %select the relevant lines
Temp=regexprep(Temp,'[^,]*,([^,]*),[^,]*','$1'); %keep only the text between the commas
Temp=str2double(Temp); %convert the text to values
plot(Time,Temp,'.')
  1 Comment
Dominic Salupo
Dominic Salupo on 19 May 2021
Thank you so much! I was stuck on that for a while, not completely sure how you came up with that out of know where lol but it's awesome thank you

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!