Sorting data from text file

2 visualizaciones (últimos 30 días)
Tyler McDonald
Tyler McDonald el 28 de Oct. de 2014
Respondida: Prateekshya el 7 de Oct. de 2024
We have to take data from a .txt file and create a for loop that searches through the data and then finds the lowest value. It then displays that value and searches for the next highest number.
What I have so far:
[Time Height] = textread('HW4_CurveFitData.txt','%f %f','delimiter',',');
t = [];
h = [];
CurrentValue = -inf;
while length(t) < length(Time)
for k = 1 : length(Time)
if Time(1) > CurrentValue
end
end
t(end+1)= CurrentValue
end
I'm getting caught up in the middle part where you have to search through the data.

Respuestas (1)

Prateekshya
Prateekshya el 7 de Oct. de 2024
Hello Tyler,
To solve this problem, you need to iterate through your data to find the lowest value, display it, and then find the next highest value iteratively. This involves keeping track of which values you've already processed, ensuring that each iteration finds the next smallest unprocessed value.
  • Data Reading: The textread function reads the data from the .txt file into two arrays, Time and Height.
  • Initialization: Arrays t and h are used to store the sorted times and corresponding heights. A logical array processedIndices keeps track of which indices have already been processed.
  • While Loop: The loop continues until all times are processed. It searches for the smallest unprocessed value in each iteration.
  • Finding Minimum: Inside the loop, iterate over the Time array to find the smallest value that hasn't been processed yet. This is done using a simple comparison against minValue.
  • Updating Results: Once the minimum is found, add it to the t and h arrays and mark the index as processed.
I hope this helps!

Categorías

Más información sobre Text Data Preparation 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