Using cellfun to pull information
조회 수: 2 (최근 30일)
이전 댓글 표시
Hopefully I can explain this!
I have the following code:
clear;
clc;
%--------------------------------------------------------------------------
% Table 1
Color1 = ["Blue"; "Orange";"Purple";"Blue";"Fuschia";"Red"];
LiscensePlate1 = [12569;12897;56783;45231;78999;99999];
t1first = datetime(2021,1,1,0,0,0);
t2first = datetime(2021,1,1,3,0,0);
tfirst = t1first:minutes(35):t2first;
tfirst=tfirst';
cars1 = table(Color1,LiscensePlate1,tfirst);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Table 2
Color2 = ["Blue"; "Orange";"Purple";"Blue";"Fuschia";"Red"];
LiscensePlate2 = [88888;12897;77777;45231;88999;99999];
t1second = datetime(2022,1,1,2,0,0);
t2second = datetime(2022,1,1,5,0,0);
tsecond = t1second:minutes(35):t2second;
tsecond=tsecond';
cars2 = table(Color2,LiscensePlate2,tsecond);
%--------------------------------------------------------------------------
For this example, say I am out watching cars on my street corner on two different days and I write down the above information (color, liscense plate number, and the time I saw the car).
Is there any way to use cellfun to search between the two tables and see if any of the liscense plate numbers match? If they match, how would I then pull the time I saw the cars and see when the difference is between the time I first saw the car and the second?
I know with a table this small and I could look for the matching liscense plates easily but I am actually dealing with a larger data set and would need this automated (cellfun) if possible!
Thank you in advance for all of your help!
댓글 수: 0
채택된 답변
Jan
2022년 7월 18일
편집: Jan
2022년 7월 18일
cellfun works on cell arrays, as the name says. You are working with tables. So this command does not match at all.
Color1 = ["Blue"; "Orange";"Purple";"Blue";"Fuschia";"Red"];
LiscensePlate1 = [12569;12897;56783;45231;78999;99999];
t1first = datetime(2021,1,1,0,0,0);
t2first = datetime(2021,1,1,3,0,0);
tfirst = t1first:minutes(35):t2first;
tfirst=tfirst';
cars1 = table(Color1,LiscensePlate1,tfirst)
% Table 2
Color2 = ["Blue"; "Orange";"Purple";"Blue";"Fuschia";"Red"];
LiscensePlate2 = [88888;12897;77777;45231;88999;99999];
t1second = datetime(2022,1,1,2,0,0);
t2second = datetime(2022,1,1,5,0,0);
tsecond = t1second:minutes(35):t2second;
tsecond=tsecond';
cars2 = table(Color2,LiscensePlate2,tsecond);
[match, index] = ismember(cars1.LiscensePlate1, cars2.LiscensePlate2)
cars1(match, :)
cars2(index(match), :)
By the way, the word is "license", not "liscense".
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!