unmatched the number of variables specified

3 次查看(过去 30 天)
Hello,
I have different test data, then I try to get plots. However, some of test data gives their plot, some of them doesn't work. For example, cycle test data to obtain the plot works whereas HPPC test doesn't work.
I have the error written below.
Error using tabular/renamevars
The number of new variable names must match the number of variables specified.
Empties = renamevars(Empties, Empties.Properties.VariableNames, ["Storage_Temperature", "Storage_Time_Months", "Notes", "Validate"]); ...
  2 个评论
Mathieu NOE
Mathieu NOE 2024-3-7
please share a working code + some files if you can
Tugce
Tugce 2024-5-21
Hi,
Here is my code. The error is in the line that is "Empties = renamevars(Empties, Empties.Properties.VariableNames, ["Storage_Temperature", "Storage_Time_Months", "Notes", "Validate"]); ...
% Add empty variables - this will need updating for the option to input storage temp, time and add notes
Empties = num2cell(NaN(height(HPPC_Output), length(COL) - width(HPPC_Output))); % Create empty array for empty variables required on output
Empties = cell2table(Empties); % Convert to table
Empties = renamevars(Empties, Empties.Properties.VariableNames, ["Storage_Temperature", "Storage_Time_Months", "Notes", "Validate"]); ...
% Add required variables
HPPC_Output = [HPPC_Output Empties]; % Add to output table
% Re-arrange variable locations
VariableLocations = ([COL; HPPC_Output.Properties.VariableNames; ...
num2cell([3:14 22 36 28 33 16:19 24 23 25 20 21 15 1 30 29 27 32 26 31 34 35 37 2])])'; % Manually input location of variables wrt COL - future work includes automating this
VariableLocations = cell2table(VariableLocations); % Convert to table
VariableLocations = sortrows(VariableLocations, 3, "Ascend"); % Sort so numbers are ascending
for i = 1:height(VariableLocations)
HPPC_Output = movevars(HPPC_Output, VariableLocations{i,2}, 'After', width(HPPC_Output)); % Move each variable to the...
% end of the table until they are in order
end
% Finalise UIC
for i = 1:height(HPPC_Output)
j = num2str(i); % Convert row number, i, to a string
HPPC_Output.UIC(i,:) = append(HPPC_Output.UIC(i,:), j); % Append string to the end of UIC
end
% Renaming the table variables to match COL
VariableNames = HPPC_Output.Properties.VariableNames;
HPPC_Output = renamevars(HPPC_Output, VariableNames, COL);

请先登录,再进行评论。

回答(2 个)

Florian Bidaud
Florian Bidaud 2024-3-7
编辑:Florian Bidaud 2024-3-7
The error is self explanatory, Empties.Properties.VariableNames returns a cell array of all the variable names. You try to replace them by ["Storage_Temperature", "Storage_Time_Months", "Notes", "Validate"]. so 4 variables. It means the table 'Empties' does not have 4 variables.
I suspect in your case one of the variables was empty and then got removed from the table.

Tugce
Tugce 2024-3-14
Hi,
Thank you for your comments.
I solved the error while deleting 2 variables. Because there are 2 coloumbs in the empties file. If I delete any 2 variable, it works.
For example, if I write like that " Empties = renamevars(Empties, Empties.Properties.VariableNames, ["Storage_Temperature", "Storage_Time_Months"]); ... " with just 2 varilable, then I obtained the table in the picture "Variables_Empties_2 coulomb". However, there is still no result because of another error. Before solving another error, I would like to add 2 more coulomb for "Notes" and "Validate" since I need its value. But I cannot add 2 more coulomb in empties file. How can I add?
Also, you can find the code's screenshot attached.

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by