21 views (last 30 days)

Show older comments

wesleynotwise on 2 Aug 2017

Commented: wesleynotwise on 3 Aug 2017

Accepted Answer: Steven Lord

Open in MATLAB Online

Hello. I wanted to add a new column from a (500 x 4) Table, with some conditions, to another (500 x 13) Table. Not so sure why I get this warning sign, though they have the same number of row:

Warning: The new variables being added to the table have fewer rows than

the table. They have been extended with rows containing default values.

> In tabular/subsasgnDot (line 327)

In tabular/subsasgn (line 67)

Here is my code:

for i=1:size(Table1.Std,1); % count the number of rows in Table 1 (500x4)

if Table1.Std(i) > 10 || Table1.Std(i) <-10; % conditions applied

Table2.NewStd(i) =1; % adding a new column in Table 2

else Table2.NewStd(i) =0;

end

end

##### 0 Comments Show -2 older commentsHide -2 older comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

### Accepted Answer

Steven Lord on 2 Aug 2017

Open in MATLAB Online

What is size(1, 1)? The scalar 1 has exactly 1 row. Since you receive that warning the table Table2 must have more than 1 row. As a simpler example:

>> T = table((1:3).', 'VariableNames', {'Variable1'});

>> numberOfRows = height(T)

numberOfRows =

3

>> T.Variable2(1) = 17

Warning: The new variables being added to the table have fewer rows than the table.

They have been extended with rows containing default values.

In this case I added a new variable with 1 row to a table with 3 rows. The new variable must contain the same number of rows as the existing variable(s) so MATLAB issues a warning and fills in the extra spaces. Since the new variable contains double precision data, the default value used to fill in is 0.

In this case, you can use logical indexing.

% Preallocate

T.Variable3 = zeros(height(T), 1);

% Update

T.Variable3(T.Variable2 > 0) = 42

or create the variable of the correct size first, then add the whole variable in one statement.

V4 = T.Variable1+T.Variable2-T.Variable3;

T.Variable4 = V4

##### 5 Comments Show 3 older commentsHide 3 older comments

Show 3 older commentsHide 3 older comments

wesleynotwise on 2 Aug 2017

#### Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/351237-adding-a-new-column-to-a-table-but-get-a-warning#comment_473989

Open in MATLAB Online

Hi Steven, Thanks for your reply. I also thought that my Table 2 has more rows than Table 1, but I have check few times, both the tables have the same number of rows, that is 500. But I still receive the warning, which makes me wonder.

What I did now was, I first put Table1.Std in Table 2 as a new column, and then did the data processing in my Table 2. I have no problem in the first step, but when I run the conditional code, the same problem occurs. The new code looks like this

% No problem for this

Table2.NewStd = Table1.Std; % new column created in T2, data from T1

% warning sign appears when I run this

for i=1:size(Table2.NewStd,1);

if Table2.NewStd(i) > 10 || Table2.NewStd(i) <-10;

Table2.NewStdCon(i) =1;

else Table2.NewStdCon(i) =0;

end

end

Any clue?

Steven Lord on 2 Aug 2017

#### Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/351237-adding-a-new-column-to-a-table-but-get-a-warning#comment_474016

Open in MATLAB Online

Add a line displaying Table2 immediately prior to entering the for loop. How many rows does the variable NewStdCon inside Table2 have on that line? If you said that is a trick question, Table2 doesn't have a variable named NewStdCon on that line, you're correct.

Now continue executing the code until you see that warning. It's going to be one of the lines where you assign either 1 or 0 to Table2.NewStdCon(i). What is i on that line -- is it equal to the height of Table2? If it is not, you're trying to add a new variable to the table with fewer rows than the table has rows, just as the warning says.

If you had filled in all of Table2.NewStdCon prior to entering that for loop, you wouldn't receive that warning.

Table2.NewStdCon = false(height(Table2), 1);

wesleynotwise on 2 Aug 2017

#### Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/351237-adding-a-new-column-to-a-table-but-get-a-warning#comment_474031

Open in MATLAB Online

Sorry, I don't quite follow what you said. i is the number of rows of Table2.NewStd. My logic of the code is like this:

Step 1: Read row No.1 of Table2.NewStd.

Step 2: If the data >10 or <-10, return 1 in row No.1 of Table2.NewStdCon, which is a new column.

Step 3: Else, return 0 in row No.1 Table2.NewStdCon.

Step 4: Now read row No.2 of Table2.NewStd.

Step 5: Repeat Step 2, but return in Row No. 2 of Table2.NewStdCon.

Step 6: ...

Can you please show me where to put this code from your recommendation? And what does it mean actually? (I'm still not quite familiar with MATLAB)

Table2.NewStdCon = false(height(Table2), 1);

Steven Lord on 2 Aug 2017

#### Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/351237-adding-a-new-column-to-a-table-but-get-a-warning#comment_474048

Open in MATLAB Online

No, i is not the number of rows of Table2.NewStd, not all the time.

for i = 1:10

disp(i)

end

In this example, i will take on values from the vector 1:10. For one iteration, it will be 2. For another iteration, it will be 7. For the first iteration, it is 1.

Here's an example that does not involve a table that will hopefully clarify things.

A = [1; 2; 3; 4; 5]

A(1, 2) = 42

What is the size of A after executing these two commands?

I never assigned anything to A(4, 2) but it has a value. What is that value? Why does that element have that value?

This behavior is the same as the behavior for table, except table issues a warning when it grows a variable like that.

wesleynotwise on 3 Aug 2017

#### Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/351237-adding-a-new-column-to-a-table-but-get-a-warning#comment_474103

Thanks again for your reply. I think I begin to understand what you tried to explain to me, but please let me me first sort few things out.

1. Sorry, my reply to i was wrong, it is a running number from 1 to the number of rows of Table2.NewStd (in my case, it is 1 to 500).

2. I don't understand this: " For one iteration, it will be 2. For another iteration, it will be 7. For the first iteration, it is 1."

3. I think the example is excellent. Although I still don't quite understand, I roughly know where the logic behind.

4. So your recommendation is to first fill Table2.NewStdCon with '0', and then replace by the output of my loop, right? Is there any other method?

Sign in to comment.

### More Answers (0)

Sign in to answer this question.

### See Also

### Categories

MATLABLanguage FundamentalsData TypesTables

Find more on **Tables** in Help Center and File Exchange

### Tags

- table
- if else statement
- adding column

### 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.

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