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

7 vues (au cours des 30 derniers jours)
AIDAN DEITCH
AIDAN DEITCH le 18 Fév 2021
Modifié(e) : dpb le 18 Fév 2021
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 commentaires
Bob Thompson
Bob Thompson le 18 Fév 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 le 18 Fév 2021
Okay sorry for incorrect formatting this is my first time posting here. Thank you for the reply.

Connectez-vous pour commenter.

Réponses (1)

dpb
dpb le 18 Fév 2021
Modifié(e) : dpb le 18 Fév 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! :)

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by