フィルターのクリア

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

5 ビュー (過去 30 日間)
Harald von der Osten
Harald von der Osten 2024 年 5 月 8 日
移動済み: Voss 2024 年 5 月 8 日
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
Harald von der Osten 2024 年 5 月 8 日
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
Harald von der Osten 2024 年 5 月 8 日
移動済み: 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