How do I add column headers (variable names) to a dummyvar table?

I could use some help adding column headers (variable names) to a dummyvar table? My code is attached. The table (tbl) once converted to an array, below, shows only 1 column header, but I need to show 4 distinct column headers and then add that matrix to another table. Thank you.
tempSector = dummyvar(categorical(ImpactWind));
WindOther = tempSector(:,1);
WindRail = tempSector(:,2);
WindAirport = tempSector(:,3);
WindRoad = tempSector(:,4);
tbl = table(WindOther,WindRail,WindAirport,WindRoad);
% tbl.Properties.VariableNames = {'WindOther','WindRail','WindAirport','WindRoad'};
IW = tbl{:,:}; % IW = table of ImpactWind dummyvars
CMET4 = addvars(CMET3, IW); % CMET3 is a matrix of other meterological parameters

8 件のコメント

Matt J
Matt J 2023 年 6 月 13 日
please Run your code, so that we have both the input and output.
Douglas Leaffer
Douglas Leaffer 2023 年 6 月 13 日
Douglas Leaffer
Douglas Leaffer 2023 年 6 月 13 日
Output of code: Column 12 should be extracted to 4 separate columns (not 1), each with header name
Let me try to run it:
tempSector = dummyvar(categorical(ImpactWind));
Unrecognized function or variable 'ImpactWind'.
WindOther = tempSector(:,1);
WindRail = tempSector(:,2);
WindAirport = tempSector(:,3);
WindRoad = tempSector(:,4);
tbl = table(WindOther,WindRail,WindAirport,WindRoad);
% tbl.Properties.VariableNames = {'WindOther','WindRail','WindAirport','WindRoad'};
IW = tbl{:,:}; % IW = table of ImpactWind dummyvars
CMET4 = addvars(CMET3, IW); % CMET3 is a matrix of other meterological parameters
Oops! It looks like you forgot to attach data. We'll check back later. In the meantime, setting properties and using addvars should work.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Deepak Gupta
Deepak Gupta 2023 年 6 月 13 日
@Image Analyst I don't think you need data to understand the problem user is trying to demostrate. It's evidently clear from the output and writeup he has written.
Deepak Gupta
Deepak Gupta 2023 年 6 月 13 日
@Douglas Leaffer Simplest way to do it is convert your table back to an array using x = table2array(CMET4) and than convert back to a table y = array2table(x). It will separate out each column. You can assign new variable names using y.Properties.VariableNames = {'a' 'b' 'c' 'd' 'e'}
Matt J
Matt J 2023 年 6 月 13 日
編集済み: Matt J 2023 年 6 月 13 日
please Run your code here in the forum, using the instructions in the link I gave you. Example,
T=array2table(rand(5,3))
T = 5×3 table
Var1 Var2 Var3 _______ _______ _______ 0.7859 0.65929 0.65119 0.25801 0.1189 0.7871 0.1786 0.42362 0.54381 0.84239 0.74138 0.20506 0.79453 0.51149 0.8186
newvar=rand(5,1);
T=addvars(T,newvar)
T = 5×4 table
Var1 Var2 Var3 newvar _______ _______ _______ ________ 0.7859 0.65929 0.65119 0.91629 0.25801 0.1189 0.7871 0.82619 0.1786 0.42362 0.54381 0.16378 0.84239 0.74138 0.20506 0.1588 0.79453 0.51149 0.8186 0.088069
Douglas Leaffer
Douglas Leaffer 2023 年 6 月 13 日
Thank you @Deepak Gupta

サインインしてコメントする。

回答 (1 件)

Douglas Leaffer
Douglas Leaffer 2023 年 6 月 13 日
移動済み: Image Analyst 2023 年 6 月 13 日

0 投票

Code file is too large to upload ... ... I did it the 'brute force' method
WindOther = tbl.WindOther; WindRail = tbl.WindRail;
WindAirport = tbl.WindAirport; WindRoad = tbl.WindRoad;
IW = table2array(tbl,'VariableNames', {'WindOther','WindRail','WindAirport','WindRoad'});
%IW = tbl{:,:}; % table of wind impact dummyvars
CMET4 = addvars(CMET3, WindAirport, WindRoad, WindRail, WindOther );

カテゴリ

製品

リリース

R2022a

質問済み:

2023 年 6 月 13 日

編集済み:

2023 年 6 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by