How to Compare multiple rows of the Same txt file at the same time.

3 views (last 30 days)
I have a txt file that contains a time column and a distance column. I am trying to write a script that will find the greatest absolute differnce between any 2 consectuive distance values and return that value. I am having trouble figuring out how to accsess two rows of a list at the same time. Any help is greatly appriceated and I have posted my code below.
  2 Comments
Bob Thompson
Bob Thompson on 18 Feb 2021
Please copy the actual code, rather than a picture of it.
Is 'numI' supposed to always be 0?
If I'm understanding you correctly, you want to take the difference between all consecutive values, and then find the largest difference? I would recommend using the loop to read the entire file, index the results, and then do a quick matrix math to find difference and max. Something like the following:
count = 0;
while ~feof(fConn)
count = count + 1;
cLine = fgetl(fConn);
parts = strsplit(cLine,',');
numF(count) = str2double(parts(2));
end
differences = abs(numF(1:end-1)-numF(2:end));
maxdiff = max(differences);
AIDAN DEITCH
AIDAN DEITCH on 18 Feb 2021
Okay sorry for incorrect formatting this is my first time posting here. Thank you for the reply.

Sign in to comment.

Answers (1)

dpb
dpb on 18 Feb 2021
Edited: dpb on 18 Feb 2021
Read the file(s) into memory with importdata or other high-level function appropriate to the format; then use diff() on the second column (presuming time is first) and max() with optional location parameter to return the position. Remember the length will be one less than original array length.
<Import Text-files> is link to documentation on bringing in text files in MATLAB. It's much simpler than you're making it! :)

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!