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
채택된 답변
Star Strider
2021년 6월 21일
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
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.
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.
.
Many thanks @Star Strider
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
