Using cellfun to pull information

4 visualizaciones (últimos 30 días)
John Doe
John Doe el 18 de Jul. de 2022
Editada: Jan el 18 de Jul. de 2022
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!

Respuesta aceptada

Jan
Jan el 18 de Jul. de 2022
Editada: Jan el 18 de Jul. de 2022
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)
cars1 = 6×3 table
Color1 LiscensePlate1 tfirst _________ ______________ ____________________ "Blue" 12569 01-Jan-2021 00:00:00 "Orange" 12897 01-Jan-2021 00:35:00 "Purple" 56783 01-Jan-2021 01:10:00 "Blue" 45231 01-Jan-2021 01:45:00 "Fuschia" 78999 01-Jan-2021 02:20:00 "Red" 99999 01-Jan-2021 02:55:00
% 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)
match = 6×1 logical array
0 1 0 1 0 1
index = 6×1
0 2 0 4 0 6
cars1(match, :)
ans = 3×3 table
Color1 LiscensePlate1 tfirst ________ ______________ ____________________ "Orange" 12897 01-Jan-2021 00:35:00 "Blue" 45231 01-Jan-2021 01:45:00 "Red" 99999 01-Jan-2021 02:55:00
cars2(index(match), :)
ans = 3×3 table
Color2 LiscensePlate2 tsecond ________ ______________ ____________________ "Orange" 12897 01-Jan-2022 02:35:00 "Blue" 45231 01-Jan-2022 03:45:00 "Red" 99999 01-Jan-2022 04:55:00
By the way, the word is "license", not "liscense".

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by