Need help reading from arrays and finding corresponding value

2 次查看(过去 30 天)
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

采纳的回答

Saeid
Saeid 2018-12-11
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;

更多回答(1 个)

Bob Thompson
Bob Thompson 2018-12-11
编辑:Bob Thompson 2018-12-11
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 个评论
Saeid
Saeid 2018-12-11
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 2018-12-11
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));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by