フィルターのクリア

Need help reading from arrays and finding corresponding value

2 ビュー (過去 30 日間)
barry conrad
barry conrad 2018 年 12 月 11 日
コメント済み: Bob Thompson 2018 年 12 月 11 日
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 Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by