How to ensure that MATLAB reads the first column as a column in a t... (2024)

83 views (last 30 days)

Show older comments

alphabetagamma on 30 Jul 2022

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table

Commented: dpb on 31 Jul 2022

Accepted Answer: Cris LaPierre

coefficient_tab = cell2table(cell(0,5), 'VariableNames', {'Name', 'Estimate', 'SE', 'tStat', 'pValue'});

bundle = ['AB'; 'CD'; 'EF'; 'GH'];

for i = 1:length(bundle)

name = bundle(i, :);

tab = table([value1(i); value2(i)],... % elements of first column

[NaN; NaN],... % elements of second column

[NaN; NaN],...

[NaN; NaN],...

'VariableNames',{'Estimate', 'SE', 'tStat', 'pValue'},...

'RowNames',{strcat('var1_', name), strcat('var2_', name)});

coefficient_table = [coefficient_table; tab];

end

Here's a snapshot of the output from "tab" when i = AB

Estimate SE tStat pValue

________ ___ _____ ______

var1_AB 1.0693 NaN NaN NaN

var2_AB 2.1268 NaN NaN NaN

The output of tab is a table with 4 columns. The columns have values on 'estimate', 'SE', 'tstat', 'pValue'. However, MATLAB is not reading the column with the variable names as a column. So, instead of having 5, there are 4 columns. How can I adjust "tab" to ensure that the output has 5 columns and the first column is named as "Name" as mentioned in "coefficient_tab"?

So, I would like the table to look like:

Name Estimate SE tStat pValue

_____ ________ ___ _____ ______

var1_AB 1.0693 NaN NaN NaN

var2_AB 2.1268 NaN NaN NaN

I tried adding "Name" in "tab", but that didn't work

tab = table([value1(i); value2(i)],... % elements of first column

[NaN; NaN],... % elements of second column

[NaN; NaN],...

[NaN; NaN],...

'VariableNames',{'Name','Estimate', 'SE', 'tStat', 'pValue'},...

'RowNames',{strcat('var1_', name), strcat('var2_', name)})

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Cris LaPierre on 30 Jul 2022

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#answer_1018360

You are using 'RowNames', which are not considered a table column. You therefore either need to use rownames everywhere (original table is 0x4) or you need to create a 5th variable containing the names and not use rownames.

% Using RowNames

coefficient_table = cell2table(cell(0,4), 'VariableNames', {'Estimate', 'SE', 'tStat', 'pValue'});

bundle = ['AB'; 'CD'; 'EF'; 'GH'];

value1 = rand(1,4);

value2 = rand(1,4);

for i = 1:length(bundle)

name = bundle(i, :);

coefficient_table = [coefficient_table; [{value1(i); value2(i)},... % elements of first column

{NaN; NaN},... % elements of second column

{NaN; NaN},...

{NaN; NaN}]];

end

coefficient_table.Properties.RowNames = ["var1_";"var2_"] + string(bundle)'

coefficient_table = 8×4 table

Estimate SE tStat pValue ________ ___ _____ ______ var1_AB 0.058393 NaN NaN NaN var2_AB 0.71082 NaN NaN NaN var1_CD 0.39971 NaN NaN NaN var2_CD 0.65425 NaN NaN NaN var1_EF 0.9427 NaN NaN NaN var2_EF 0.066835 NaN NaN NaN var1_GH 0.14773 NaN NaN NaN var2_GH 0.73154 NaN NaN NaN

Now the same code but using a 5th variable instead

coefficient_table = cell2table(cell(0,5), 'VariableNames', {'Name','Estimate', 'SE', 'tStat', 'pValue'});

bundle = ['AB'; 'CD'; 'EF'; 'GH'];

value1 = rand(1,4);

value2 = rand(1,4);

for i = 1:length(bundle)

name = bundle(i, :);

coefficient_table = [coefficient_table; ...

[{"var1_"+ name;"var2_"+ name},...

{value1(i); value2(i)},... % elements of first column

{NaN; NaN},... % elements of second column

{NaN; NaN},...

{NaN; NaN}]];

end

coefficient_table

coefficient_table = 8×5 table

Name Estimate SE tStat pValue _________ ________ ___ _____ ______ "var1_AB" 0.48427 NaN NaN NaN "var2_AB" 0.94618 NaN NaN NaN "var1_CD" 0.3964 NaN NaN NaN "var2_CD" 0.8816 NaN NaN NaN "var1_EF" 0.41481 NaN NaN NaN "var2_EF" 0.46532 NaN NaN NaN "var1_GH" 0.43444 NaN NaN NaN "var2_GH" 0.23924 NaN NaN NaN

1 Comment

Show -1 older commentsHide -1 older comments

alphabetagamma on 31 Jul 2022

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2292870

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2292870

Thank you for showing the codes as demo. This was helpful

Sign in to comment.

More Answers (2)

Walter Roberson on 30 Jul 2022

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#answer_1018355

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#answer_1018355

MATLAB does not put a title on RowNames. If you want a title for that column, you will need to treat it as any other variable.

By the way: if you take an existing table, and you assign

tab(end+1, :) = EXPRESSION

and EXPRESSION is a cell array with as many entries as there are columns, then MATLAB will append the content of the cell entries as a new row in the table. So you could

tab(end+1,:) = {strcat('var1_', name), value1(i), nan, nan, nan};

tab(end+1,:) = {strcat('var2_', name), value2(i), nan, nan, nan};

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

dpb on 30 Jul 2022

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#answer_1018370

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#answer_1018370

rownames are table metadata; they are NOT a variable which is why the column variable heading doesn't show up for them -- the table has only four columns of data, not five. The RowNames property of the table returns the values or you can use the following --

tab.(tab.Properties.DimensionNames{1})

See table documentation about RowNames for details.

If you want the names to be a variable, then you have to create the variable that contains them.

You can construct the above table in much more succinct fashion, however --

vnames={'Estimate', 'SE', 'tStat', 'pValue'};

rnames=["Var"+repmat(string([1;2]),numel(bundle)/2,1)+"_"+string(char(kron(bundle,ones(2,1))))];

data=[abs(randn(8,1)) nan(8,3)];

tT=array2table(data,'VariableNames',vnames,'RowNames',rnames);

produces

>> tT

tT =

8×4 table

Estimate SE tStat pValue

________ ___ _____ ______

Var1_AB 0.21916 NaN NaN NaN

Var2_AB 1.0458 NaN NaN NaN

Var1_CD 0.95098 NaN NaN NaN

Var2_CD 0.7948 NaN NaN NaN

Var1_EF 0.07143 NaN NaN NaN

Var2_EF 0.77369 NaN NaN NaN

Var1_GH 0.77416 NaN NaN NaN

Var2_GH 0.26565 NaN NaN NaN

>>

4 Comments

Show 2 older commentsHide 2 older comments

alphabetagamma on 31 Jul 2022

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2292875

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2292875

I see. Thanks a lot. Although a little complicated, thanks for showing me how to write the code in more succinct format

dpb on 31 Jul 2022

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2293350

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2293350

Seems far less "complicated" than all the machinations through for loop -- and the first creation that is then dynamically reallocated in the for loop is very inefficient. For small tables it won't be particularly noticeable, but presuming the real case is significantly larger the hit may become sizable.

I got just a little cute in the creation of the row names to duplicate the char() string, granted -- "just playing" to give a little "push" to the learning curve; use string arrays with compose() and perhaps an arrayfun() to build the rownames vector in a more straightforward, "dead-ahead" programming style, but the key illustration is to build the data array first and array2table it, don't just add a single record at a time dynamically reallocating the whole table every time through the loop.

There are times when it's about the only way to proceed without a lot of extra effort when one doesn't have the next record yet, but this isn't a case of that; by the example, you already have all the data excepting the unknowns; it would be the case you don't have those until some other calculation that might call for it.

I've submitted enhancement request for an append function to specifically allow one to insert an empty record into an existing table where it is smart enough to know the types and build it -- so far, TMW hasn't seen fit to implement it.

alphabetagamma on 31 Jul 2022

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2293535

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2293535

Oh I see. That explains why my code runs so slowly when the increase the number of elements in "bundle" to 30, instead of just 4. Thanks for the explanation. I'll try to implement it.

dpb on 31 Jul 2022

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2293630

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/1771280-how-to-ensure-that-matlab-reads-the-first-column-as-a-column-in-a-table#comment_2293630

Bound to be at least a part of it, anyways, yes...although with still only 60 lines (unless the multipler of number of records/type is also gone up to a higher number than2) I'd not expect it to be all that big of an effect yet.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and Arrays

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

  • table
  • for loop
  • function

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to ensure that MATLAB reads the first column as a column in a t... (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • 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
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to ensure that MATLAB reads the first column as a column in a t... (2024)
Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5574

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.