Clear Filters
Clear Filters

unmatched the number of variables specified

13 views (last 30 days)
Tugce
Tugce on 7 Mar 2024
Commented: Tugce on 21 May 2024 at 12:43
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 Comments
Mathieu NOE
Mathieu NOE on 7 Mar 2024
please share a working code + some files if you can
Tugce
Tugce on 21 May 2024 at 12:43
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);

Sign in to comment.

Answers (2)

Florian Bidaud
Florian Bidaud on 7 Mar 2024
Edited: Florian Bidaud on 7 Mar 2024
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 on 14 Mar 2024
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.

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!