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

5 visualizaciones (últimos 30 días)
AIDAN DEITCH
AIDAN DEITCH el 18 de Feb. de 2021
Editada: dpb el 18 de Feb. de 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 comentarios
Bob Thompson
Bob Thompson el 18 de Feb. de 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 el 18 de Feb. de 2021
Okay sorry for incorrect formatting this is my first time posting here. Thank you for the reply.

Iniciar sesión para comentar.

Respuestas (1)

dpb
dpb el 18 de Feb. de 2021
Editada: dpb el 18 de Feb. de 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! :)

Categorías

Más información sobre Variables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by