Add column with values to table based on value in existing column (look up)

7 vues (au cours des 30 derniers jours)
Jack
Jack le 29 Juil 2025
Commenté : Jack le 30 Juil 2025
I want to add a column to the end of Matrix.csv (my actual data is irregular and 18000+ lines) that matches the value in the Direction column to the heading below and outputs the corresponding wDir value.
I am trying to produce something like an excel lookup function after creating headingTable through the below code.
heading = ["000", "015", "030", "045", "060", "075", "090", "105", "120", "135", "150", "165", ...
"180", "195", "210", "225", "240", "255", "270", "285", "300", "315", "330", "345"];
wDir = [90 75 60 45 30 15 0 345 330 315 300 295 270 255 240 225 210 195 180 165 150 135 120 105];
count = 0;
for i =1:numel(wDir)
heading1 = heading(i);
wDir1 = wDir(i);
outData = [heading1 wDir1];
count =count + 1;
headingTable(count,:) = outData;
end

Réponse acceptée

Stephen23
Stephen23 le 29 Juil 2025
Modifié(e) : Stephen23 le 29 Juil 2025
This is MATLAB so forget about Excel and loops.
Use e.g. ISMEMBER or DISCRETIZE:
T = readtable('Matrix.csv')
T = 23×4 table
speed Direction Height Period _____ _________ ______ ______ 6 0 7.18 13.98 6 0 6.7 15.21 6 0 7.1 15.67 6 15 0.96 4.94 6 15 1.02 6.03 6 60 5.86 13.57 6 60 6.12 14.25 6 255 1.91 15 6 330 4.14 16.32 6 330 5.07 5.05 9 90 7.16 15.52 12 75 2 4.98 12 105 2.05 7.96 15 105 3.1 10.91 15 120 2.92 7.31 15 120 3.06 7.83
H = [0,15,30,45,60,75,90,105,120,135,150,165,180,195,210,225,240,255,270,285,300,315,330,345,360];
W = [90,75,60,45,30,15,0,345,330,315,300,295,270,255,240,225,210,195,180,165,150,135,120,105];
T.WDir = discretize(T.Direction,H,W)
T = 23×5 table
speed Direction Height Period WDir _____ _________ ______ ______ ____ 6 0 7.18 13.98 90 6 0 6.7 15.21 90 6 0 7.1 15.67 90 6 15 0.96 4.94 75 6 15 1.02 6.03 75 6 60 5.86 13.57 30 6 60 6.12 14.25 30 6 255 1.91 15 195 6 330 4.14 16.32 120 6 330 5.07 5.05 120 9 90 7.16 15.52 0 12 75 2 4.98 15 12 105 2.05 7.96 345 15 105 3.1 10.91 345 15 120 2.92 7.31 330 15 120 3.06 7.83 330
Even better would be to avoid those fiddly vectors and functions entirely:
T.Wsimple = mod(90-T.Direction,360)
T = 23×6 table
speed Direction Height Period WDir Wsimple _____ _________ ______ ______ ____ _______ 6 0 7.18 13.98 90 90 6 0 6.7 15.21 90 90 6 0 7.1 15.67 90 90 6 15 0.96 4.94 75 75 6 15 1.02 6.03 75 75 6 60 5.86 13.57 30 30 6 60 6.12 14.25 30 30 6 255 1.91 15 195 195 6 330 4.14 16.32 120 120 6 330 5.07 5.05 120 120 9 90 7.16 15.52 0 0 12 75 2 4.98 15 15 12 105 2.05 7.96 345 345 15 105 3.1 10.91 345 345 15 120 2.92 7.31 330 330 15 120 3.06 7.83 330 330
  1 commentaire
Jack
Jack le 30 Juil 2025
Many thanks, that solves my issues.
I have some additional columns that are sadly not as simple as your mod() suggestion and discretize works perfectly

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by