assign name row to table (2024)

129 views (last 30 days)

Show older comments

Luca Re on 31 May 2023

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table

Commented: Walter Roberson on 1 Jun 2023

assign name row to table (2)

Sistemi_check=table(string(K_Nome),K_idxInstrument');

LastName = {"Sanchez";"Johnson"};

Sistemi_check.Properties.RowNames=LastName;

>> check_Instrument

Error using .

The RowNames property must be a string array or a cell array, with each name containing one or more

characters.

Error in check_Instrument (line 22)

Sistemi_check.Properties.RowNames=LastName;

2 Comments

Show NoneHide None

Walter Roberson on 31 May 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766574

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766574

You are trying to assign exactly two row names, but there needs to be one row name entry for each row.

You have at least 30 rows in your table, so you need at least 30 row-name entries.

Luca Re on 31 May 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766579

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766579

Edited: Walter Roberson on 1 Jun 2023

ok i want to rename columns in a table..is correct to use VariableNames? i try it but i get an error

Sistemi_check=table(string(K_Nome),K_idxInstrument');

LastName = {"Sanchez";"Johnson"};

Sistemi_check.Properties.VariableNames=LastName;

Error using .

The VariableNames property is a cell array of character vectors. To assign multiple variable names,

specify nonempty names in a string array or a cell array of character vectors.

Error in check_Instrument (line 22)

Sistemi_check.Properties.VariableNames=LastName;

Sign in to comment.

Sign in to answer this question.

Answers (2)

Walter Roberson on 31 May 2023

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#answer_1247984

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#answer_1247984

Either use

LastName = ["Sanchez";"Johnson"];

Sistemi_check.Properties.RowNames=LastName;

or else

LastName = {'Sanchez';'Johnson'};

Sistemi_check.Properties.RowNames=LastName;

That is you can use a cell array of character vectors, or you can use a string() array, but you cannot use a cell array of string() objects.

3 Comments

Show 1 older commentHide 1 older comment

Luca Re on 31 May 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766594

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766594

i try

LastName = ["Sanchez";"Johnson"];

and it run correcly

if i write : LastName = {'Sanchez';'Johnson'}; it give me an error

thanks

VBBV on 1 Jun 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766719

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2766719

@Luca Re, use the cell array of character vectors as shown below

% use the cell array as shown below

LastName = {'Sanchez','Johnson'};

Sistemi_check.Properties.RowNames=LastName;

By default, RowNames variable refers to rows in a table. So , a ; is not needed to tell table to concatenate the names vertically. See more about RowNames property RowNames

Walter Roberson on 1 Jun 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2767944

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#comment_2767944

Sistemi_check = table("first", 123)

Sistemi_check = 1×2 table

Var1 Var2 _______ ____ "first" 123

LastName = ["Sanchez";"Johnson"];

Sistemi_check.Properties.VariableNames=LastName

Sistemi_check = 1×2 table

Sanchez Johnson _______ _______ "first" 123

Sistemi_check = table("first", 123)

Sistemi_check = 1×2 table

Var1 Var2 _______ ____ "first" 123

LastName = {'Sanchez';'Johnson'};

Sistemi_check.Properties.VariableNames=LastName

Sistemi_check = 1×2 table

Sanchez Johnson _______ _______ "first" 123

Sign in to comment.

Sulaymon Eshkabilov on 31 May 2023

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#answer_1247994

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1976449-assign-name-row-to-table#answer_1247994

Here is a plain example how to rename row and column names in a table array:

t1 = date;

t2 = '01-June-2023';

t3 = '10-June-2023';

t4 = '20-June-2023';

T = {t1; t2; t3;t4};

D = table(T);

D.N = [10:10:40]'

D = 4×2 table

T N ________________ __ {'31-May-2023' } 10 {'01-June-2023'} 20 {'10-June-2023'} 30 {'20-June-2023'} 40

% Add row names:

LastName = {'Sanchez';'Johnson'; 'Brown'; 'Green'};

D.Properties.RowNames=LastName

D = 4×2 table

T N ________________ __ Sanchez {'31-May-2023' } 10 Johnson {'01-June-2023'} 20 Brown {'10-June-2023'} 30 Green {'20-June-2023'} 40

% Change column names:

Col_Name = {'Date', 'Number'};

D=renamevars(D,{'T', 'N'}, Col_Name)

D = 4×2 table

Date Number ________________ ______ Sanchez {'31-May-2023' } 10 Johnson {'01-June-2023'} 20 Brown {'10-June-2023'} 30 Green {'20-June-2023'} 40

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsData TypesTables

Find more on Tables in Help Center and File Exchange

Tags

  • assign name row to table

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.


assign name row to table (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

assign name row to table (2024)
Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5678

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.