Borrar filtros
Borrar filtros

Match text in cell array and copy to another

2 visualizaciones (últimos 30 días)
AHMED FAKHRI
AHMED FAKHRI el 21 de Jun. de 2021
Comentada: Star Strider el 22 de Jun. de 2021
Hi
I have a cell array (one of many) that looks like this: on the left is the sector name. On the right is the emission value of that sector.
Let us assume the name of the cell array is E30
'Coal Mine (open)' 0.016182475
'Food & Drink' 0.004392493
'Gas Platform' 0.00377076
'Gas Terminal' 0.023197913
'Glass' 0.031353608
'LNG Terminal' 0.027425894
'NRMM' 0.421852383
'Oil Terminal' 0.093687465
'Other Iron and Steel' 0.004623789
'Other Minerals' 0.004241753
'Other industry' 6.61E-05
'Paper' 0.002562681
'Vehicles' 0.000265986
I want to copy the values only to the cooreponding exact row (sector name) and E30 column in the new array below (leaving the empty sectors as they are)
Sector E25 H25 CCS25 E30
'Coal Mine (open)'
'Food & Drink'
'Gas Platform'
'Gas Terminal'
'Glass'
'NRMM'
'Oil Terminal'
'Other Chemicals'
'Other Iron and Steel'
'Other industry'
'Paper'
'Vehicles'
'Ammonia'
'Refining'
'LNG Terminal'
'Non ferrous metal'
'Iron (Port Talbot Scunthorpe)'
'Lime'
'Oil Platform'
'Other Minerals'
'Cement'
'Ethylene'
'Compressor Station'
'Construction'
'Other Fuel Production'
'Waste Processing'
Is there please general way to do that? If you want these info above as Excel, these are attached.
Many thanks

Respuesta aceptada

Star Strider
Star Strider el 21 de Jun. de 2021
This is the best I can do with that. It will likely require a bit of editing later (with respect to the variable names).
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/659855/E30.xlsx';
T1 = readtable(filename)
T1 = 13×2 table
Var1 Var2 __________________________ __________ {''Coal Mine (open)'' } 0.016182 {''Food & Drink'' } 0.0043925 {''Gas Platform'' } 0.0037708 {''Gas Terminal'' } 0.023198 {''Glass'' } 0.031354 {''LNG Terminal'' } 0.027426 {''NRMM'' } 0.42185 {''Oil Terminal'' } 0.093687 {''Other Iron and Steel''} 0.0046238 {''Other Minerals'' } 0.0042418 {''Other industry'' } 6.6061e-05 {''Paper'' } 0.0025627 {''Vehicles'' } 0.00026599
[dir,fname,ext] = fileparts(filename)
dir = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/659855'
fname = 'E30'
ext = '.xlsx'
T1.Properties.VariableNames = {'Sector',fname}
T1 = 13×2 table
Sector E30 __________________________ __________ {''Coal Mine (open)'' } 0.016182 {''Food & Drink'' } 0.0043925 {''Gas Platform'' } 0.0037708 {''Gas Terminal'' } 0.023198 {''Glass'' } 0.031354 {''LNG Terminal'' } 0.027426 {''NRMM'' } 0.42185 {''Oil Terminal'' } 0.093687 {''Other Iron and Steel''} 0.0046238 {''Other Minerals'' } 0.0042418 {''Other industry'' } 6.6061e-05 {''Paper'' } 0.0025627 {''Vehicles'' } 0.00026599
C2 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/659860/Mapping.xlsx');
T2 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/659860/Mapping.xlsx')
T2 = 26×13 table
Sector E25 H25 CCS25 E30 H30 CCS30 E40 H40 CCS40 E50 H50 CCS50 __________________________ ___ ___ _____ ___ ___ _____ ___ ___ _____ ___ ___ _____ {''Coal Mine (open)'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Food & Drink'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {'Gas Platform'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Gas Terminal'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Glass'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {'NRMM'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Oil Terminal'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Other Chemicals'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Other Iron and Steel''} NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Other industry'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Paper'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Vehicles'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Ammonia'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Refining'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''LNG Terminal'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Non ferrous metal'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
T2 = innerjoin(T1,T2,'Keys','Sector')
T2 = 11×14 table
Sector E30_T1 E25 H25 CCS25 E30_T2 H30 CCS30 E40 H40 CCS40 E50 H50 CCS50 __________________________ __________ ___ ___ _____ ______ ___ _____ ___ ___ _____ ___ ___ _____ {''Coal Mine (open)'' } 0.016182 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Food & Drink'' } 0.0043925 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Gas Terminal'' } 0.023198 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Glass'' } 0.031354 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''LNG Terminal'' } 0.027426 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Oil Terminal'' } 0.093687 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Other Iron and Steel''} 0.0046238 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Other Minerals'' } 0.0042418 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Other industry'' } 6.6061e-05 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Paper'' } 0.0025627 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Vehicles'' } 0.00026599 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
This should get you started. Experiment further to get the result you want. See the documentation on innerjoin (and its friends) to understand how best to use them.
.
  8 comentarios
AHMED FAKHRI
AHMED FAKHRI el 22 de Jun. de 2021
Many thanks @Star Strider
Star Strider
Star Strider el 22 de Jun. de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by