How to get rid of this error when adding an element to the rows of a table?

I have a table with strings and double values. When I try to add new element in each row I will get this value. Could some one explain me what can be done?
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables have been extended with rows containing default values. > In tabular/subsasgnDot (line 338) In tabular/subsasgn (line 67) In orderMain (line 140)

3 Comments

Show us line 130 to 140 of orderMain.m.
The error is telling you that you're not giving enough values to fill all the column of your table when you add a row.
for i=1:length(rpmFixed)
DatTab.rpm(i,1)=rpmFixed(i);
DatTab.Lss1(i,1)=orderNewSlices{1,1}(1,i);
DatTab.Lss2(i,1)=orderNewSlices{1,2}(1,i);
DatTab.Lss3(i,1)=orderNewSlices{1,3}(1,i);
DatTab.Lss4(i,1)=orderNewSlices{1,4}(1,i);
DatTab.Lss5(i,1)=orderNewSlices{1,5}(1,i);
DatTab.Lss6(i,1)=orderNewSlices{1,6}(1,i);
DatTab.Lss7(i,1)=orderNewSlices{1,7}(1,i);
DatTab.Lss8(i,1)=orderNewSlices{1,8}(1,i);
DatTab.Lss9(i,1)=orderNewSlices{1,9}(1,i);
DatTab.Lss10(i,1)=orderNewSlices{1,10}(1,i);
DatTab.IMS1(i,1)=orderNewSlices{1,11}(1,i);
DatTab.IMS2(i,1)=orderNewSlices{1,12}(1,i);
DatTab.IMS3(i,1)=orderNewSlices{1,13}(1,i);
DatTab.HSS1(i,1)=orderNewSlices{1,14}(1,i);
DatTab.HSS2(i,1)=orderNewSlices{1,15}(1,i);
DatTab.Hss3(i,1)=orderNewSlices{1,16}(1,i);
end
Hello Thanks for the reply,
Here is my code. Could you suggest me what I can do to resolve that warning?

Sign in to comment.

 Accepted Answer

You're probably using the slowest and most complicated way to create a table.
tempslices = cellfun(@ctranspose, orderNewSlices, 'UniformOutput', false); %transpose the row vectors in the cell array to column vectors
DatTab = [table(rpmFixed(:), 'VariableNames', {'rpm'}), ...
cell2table(orderNewSlices, 'VariableNames', ...
[compose('Lss%d', 1:10), compose('IMS%d', 1:3), compose('HSS%d', 1:3)])]
should do the same.

9 Comments

Yes, as Guillaume says, you don't want to be creating a table by looping and assigning one element at a time, while growing it row by row.
To answer your original question, for every loop iteration, this line
DatTab.rpm(i,1)=rpmFixed(i);
ads a new row and only assigns to one variable in that row. That's what the warning is telling you. It's usually a sign that you're doing something wrong.
At the very least, you could take all of those 17 assignments and create a 1x17 cell array, and assign that to a new row of the table. But as Guillaume correctly points out, you should need a loop at all.
@Guillaume I was trying the code. It gives me an error. Could you please let me know what the error might be?
Error using cell2table (line 57) The VariableNames property must be a cell array, with each element containing one nonempty character vector.
Error in orderMain (line 190) cell2table(orderNewSlices, 'VariableNames', ...
You're probably using R2016b where compose returns string regardless of the input. In R2017a, with the syntax I used it returns a cell array of char. It's trivial to fix by converting the string array to cell array with cellstr:
DatTab = [table(rpmFixed(:), 'VariableNames', {'rpm'}), ...
cell2table(tempslices, 'VariableNames', ...
cellstr([compose('Lss%d', 1:10), compose('IMS%d', 1:3), compose('HSS%d', 1:3)]))]
Note that I took a guess about the size and shape of your inputs. It's entirely possible that I got these wrong and as a result the above code will give an error, but the idea is there: convert your inputs into a table with the table creation functions , table, array2table or cell2table.
edit: Actually the above is probably not going to produce the right result due to the construction of your cell array. This may work better:
tempslices = cellfun(@ctranspose, orderNewSlices, 'UniformOutput', false);
DatTab = table(rpmFixed(:), tempslices(:), 'VariableNames', ...
['rpm', cellstr([compose('Lss%d', 1:10), compose('IMS%d', 1:3), compose('HSS%d', 1:3)])])
I have same number of rows in rpmFixed and each cells in orderNewSlices. Yet I am getting this error message.
All variables must have the same number of rows.
Try
tempslices = cellfun(@ctranspose, orderNewSlices, 'UniformOutput', false);
DatTab = table(rpmFixed(:), tempslices{:}, 'VariableNames', ...
['rpm', cellstr([compose('Lss%d', 1:10), compose('IMS%d', 1:3), compose('HSS%d', 1:3)])])
As I said, I'm just guessing at what would work since I don't have your data. You should be able to work out the exact syntax required from the above.
The problem is rpmFixed is 102*1 double and tempslices is 1*16 cell array which contains 102*1 doubles. when it reads it says variable must have same numbers. Basically i feel it compares 102*1 with 1*16.
Yes, note that my latest code differs from the earlier one where I'd made a mistake. I've changed tempslices(:) to tempslices{:} (notice the different brackets), which properly passes the elements of the cell array as columns.
If that still doesn't work then check the output of:
size(rpmFixed)
%and
celldisp(cellfun(@size, tempslices, 'UniformOutput', false))

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!