Match text in cell array and copy to another

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

 Accepted Answer

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 Comments

Many thanks @Star Strider , I will read the innerjoin.
The output of the code you provided seems to exclude other sectors which are needed for arrays other than E30. E30 was just an example of many arrays I need to map to the sectors so I do not want to exclude the sector for certain array.
My pleasure.
The desired result was not obvious.
Those will be included as necessary when other tables are added to it using innerjoin.
Also experiment with outerjoin
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);
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 = outerjoin(T1,T2, 'Keys','Sector', 'MergeKeys',1)
T2 = 28×14 table
Sector E30_T1 E25 H25 CCS25 E30_T2 H30 CCS30 E40 H40 CCS40 E50 H50 CCS50 ___________________________________ _________ ___ ___ _____ ______ ___ _____ ___ ___ _____ ___ ___ _____ {''Ammonia'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Cement'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Coal Mine (open)'' } 0.016182 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Compressor Station'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''Ethylene'' } NaN 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 Platform'' } 0.0037708 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 {''Iron (Port Talbot Scunthorpe)''} NaN 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 {''Lime'' } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {''NRMM'' } 0.42185 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 NaN {''Oil Platform'' } NaN 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
It will still be necessary to edit the variable names.
Many thanks @Star Strider, managed to roughly get what I want as in the picture below, I will accept your answer. One final thing, do you know how to replace the empty values [] in the picture by NaN?
but doesn't seem to work
I am not certain what you are using to view the table in that image. They might already be NaN and show up as [] in the application you chose to view it. I would have to know more about how that was created.
Thanks, that is in MATLAB
I do not get the same result in R2021a.
This is less elegant than I would like it to be, however it has the virtue of working —
T3 = table({1;2;[];4;5;[];7},{8;[];10;11;[];13;[]}) % Original Table
T3 = 7×2 table
Var1 Var2 ____________ ____________ {[ 1]} {[ 8]} {[ 2]} {0×0 double} {0×0 double} {[ 10]} {[ 4]} {[ 11]} {[ 5]} {0×0 double} {0×0 double} {[ 13]} {[ 7]} {0×0 double}
Lm = cellfun(@isempty,table2cell(T3)) % Logical MAtrix
Lm = 7×2 logical array
0 0 0 1 1 0 0 0 0 1 1 0 0 1
for k = 1:size(Lm,2)
T3{Lm(:,k),k} = {NaN}; % Replace Empty Elements With 'NaN'
end
T3
T3 = 7×2 table
Var1 Var2 _______ _______ {[ 1]} {[ 8]} {[ 2]} {[NaN]} {[NaN]} {[ 10]} {[ 4]} {[ 11]} {[ 5]} {[NaN]} {[NaN]} {[ 13]} {[ 7]} {[NaN]}
My attempts to use varfun for this were all unsuccessful, so I went with what I could come up with that worked.
.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!