Borrar filtros
Borrar filtros

Need help reading from arrays and finding corresponding value

2 visualizaciones (últimos 30 días)
clear
tempData = csvread('C:\Users\');
%time
hr = 14;%12+pm
min = 42;
sec=12;
time = 3700;%example
%time = ((hr*3600)+(min*60)+sec);
if time==1,time<1839
n=1;
elseif 3678>time>1839
n=2;
else 5516>time>3678
n=3;
end
disp(tempData(n,3))
Hi im trying to use this code to read the time inputted by the person then to find the row it meets and read the 3rd column requried however with the method ive done its not working and it would be unefficent is there a faster way of doing this , thanks for the help.
piccc.png

Respuesta aceptada

Saeid
Saeid el 11 de Dic. de 2018
Hi, assuming A is your 47x5 matrix, replace your if part with this:
for i=2:size(A,1)
if time < A(i,1)
n=i-1;
break
end;
end;

Más respuestas (1)

Bob Thompson
Bob Thompson el 11 de Dic. de 2018
Editada: Bob Thompson el 11 de Dic. de 2018
When combining multiple if conditions you want to use & (and) and | (or).
For your purpose though you can just find the minimum difference instead of using an if statement.
[value,index] = min(abs(tempdata(:,1)-time));
disp(tempdata(index,3))
  2 comentarios
Saeid
Saeid el 11 de Dic. de 2018
Hi, this is beautifiul, but does not necessarily give the right interval (e.g. for time = 5500).
I would replace it with:
n = find((A(:,1)-time)> 0, 1)-1;
disp(A(n,3))
Bob Thompson
Bob Thompson el 11 de Dic. de 2018
Mmm, you are right. I misinterpreted how the data was being presented, and what was being asked. The find will definitely work, it is also possible to use max instead of min, with a little logic indexing.
[value,index] = max(tempdata((tempdata(:,1)-time)<=0,1));

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays 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