How can I insert data from different table that match a certain value?

9 vues (au cours des 30 derniers jours)
Martin Zuniga
Martin Zuniga le 22 Mar 2020
Commenté : Martin Zuniga le 23 Mar 2020
Hello everyone, is there a function similar to VLOOKUP from excel?
I have two tables, namely "Secc" and "offset".
In the table Secc, I want to populate every row that contains the value in the "Seccion" column with the columns LI, LF and RF2 from the table "offset" that matches that value.
In the image I did that by copy/paste for the first row in table "Secc" but since I have tons of rows, I would prefer to automate the process.
I suppose I could do a VLOOKUP in Excel, but I know Matlab can handle this faster, I just don't know how.
Thanks in advance.
PD. I am uploading my files just in case you find them helpful.

Réponse acceptée

Sindar
Sindar le 23 Mar 2020
% find the index of the offset.FrameNumber row matching each Secc.Seccion element
[~,idx] = ismember(Secc.Seccion,offset.FrameNumber);
if any(idx == 0)
% what to do if no matching frame?
end
% create new columns of Secc, writing in the offset rows in order
Secc(:,4:6) = offset(idx,2:4);
% update the names of these new columns with the names of the offset columns
Secc.Properties.VariableNames(4:6) = offset.Properties.VariableNames(2:4);
  1 commentaire
Martin Zuniga
Martin Zuniga le 23 Mar 2020
At first i got an error but then I realized idx had some zeros, so I erased that part and the code worked flawlessly.
I appreciate your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by