how to rename columns with names like Var2_1, Var2_2...?

5 次查看(过去 30 天)
Writing data to an xlsx file I get this:
Var1 Var2_1 Var2_2 Var2_3
01-May-2024 2213 98 111
How can I rename Var2_n ?
renamevars and output_data.Properties.VariableNames doesn't work..
Here is my code (thanks a lot for each hint!):
% Read data from 'data.xlsx'
data = readtable('data.xlsx');
% Get unique dates
unique_dates = unique(data{:, 1}); % Assuming the date column is the first column in the table
% Initialize output table
output_data = table();
% Loop through each unique date
for i = 1:length(unique_dates)
date_data = data(strcmp(data{:, 1}, unique_dates{i}), :);
% Calculate sum of numbers for each date
sum_numbers = floor(sum(date_data{:, 3:5}, 1));
% Create a new row with Date and sum_numbers
new_row = {unique_dates{i}, sum_numbers};
% Append the new row to the output table
output_data = [output_data; new_row];
end
% Write output data to 'data-new.xlsx'
writetable(output_data, 'data-new.xlsx');
  2 个评论
Yash
Yash 2024-5-8
Hi Harald,
I am unable to understand the issue that you are facing while using "renamevars". I tried it, and it works as expected.
Kindly refer to the example below:
tbl = table([1;11;111;1111],[2;22;222;2222],[3;33;333;3333],[4;44;444;4444],'VariableNames',["Var1" "Var2_2" "Var2_3" "Var2_4"])
tbl = 4x4 table
Var1 Var2_2 Var2_3 Var2_4 ____ ______ ______ ______ 1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
tbl = renamevars(tbl,["Var2_2" "Var2_3" "Var2_4"],["Var2" "Var3" "Var4"])
tbl = 4x4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
To better assist you, kindly let us know the issue that you are encountering.
Harald von der Osten
this is the error I get:
rror using tabular/renamevars (line 46)
Unrecognized table variable name 'Var1'.
Error in Counts (line 21)
output_data = renamevars(output_data,["Var1" "Var2_1" "Var2_2" "Var2_3"],["Date" "counts 1" "counts 2" "counts 3"])

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2024-5-8
编辑:Star Strider 2024-5-8
Probably the easiest way:
data.Properties.VariableNames(2:end) = {'Var2','Var3','Var4'};
Example —
data = array2table(rand(2,4), 'VariableNames',{'Var1','Var2_1','Var2_2','Var2_3'})
data = 2x4 table
Var1 Var2_1 Var2_2 Var2_3 _______ _______ _______ _______ 0.63401 0.43607 0.32244 0.37832 0.17206 0.16586 0.91634 0.60248
data.Properties.VariableNames(2:end) = {'Var2','Var3','Var4'}
data = 2x4 table
Var1 Var2 Var3 Var4 _______ _______ _______ _______ 0.63401 0.43607 0.32244 0.37832 0.17206 0.16586 0.91634 0.60248
You can also use that approach to rename all of them, if you want to.
.
EDIT — Corrected typogrpahical errors, added information.

更多回答(1 个)

Harald von der Osten
移动:Voss 2024-5-8
solved it. I had to rewrite the code, because for Matlab the triple 'Var2_1', 'Var2_2' and 'Var2_3' was just one column...
Thanks a lot to all for your help !

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by