How to Compare multiple rows of the Same txt file at the same time.
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Bob Thompson
2021-2-18
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);
回答(1 个)
dpb
2021-2-18
编辑:dpb
2021-2-18
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! :)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!