MATLAB Answers

Duplicate table variable name: 'VarName2'.

조회 수: 44(최근 30일)
Kushal Bhalla
Kushal Bhalla 2021년 5월 9일
댓글: Kushal Bhalla 2021년 5월 16일 10:39
I have 15 minute interval data of power produced on a daily basis from nine different sources (coal, gas, nuclear etc.) for the month of Jan 2012. I want data in a single column for each source, meaning 9 different columns since I have 9 sources. Currently it is a row wise data for each source for each day of the month. I am using the following code and getting an error (Duplicate table variable name: 'VarName2'.) :
ndata = data;
ndata(:,1)=[];
id = eye(9);
id = repmat(id,31,1); % 31 day
output = [];
for i = 1:9;
one = id(:,i);
one = logical(one);
temp = ndata(one,:);
output = [output temp];
end
Duplicate table variable name: 'VarName2'.
  댓글 수: 6
Kushal Bhalla
Kushal Bhalla 2021년 5월 11일
Sorry Sir for the inordinate delay in replying. I was reeling under a bout of illness. Had symptoms akin to COVID, but now feeling better.
Sir, you are right, I am trying to have a 2976 * 9 matrix. The columns will represent the 9 sources of power production. The rows will showcase the power produced from each source at a fifteen minute interval for the month of Jan 2012.
I was using the following code which generated an error in the for loop statement as previously shared with you.
ndata = data;
ndata(:,1)=[];
id = eye(9);
id = repmat(id,31,1); % 31 day
output = [];
for i = 1:9;
one = id(:,i);
one = logical(one);
temp = ndata(one,:);
output = [output temp];
end
d = 96;
biomass = output(:,1:d);
coal = output(:,d+1:d*2);
gas= output(:,d*2+1:d*3);
gas2 = output(:,d*3+1:d*4);
hydro = output(:,d*4+1:d*5);
nuclear = output(:,d*5+1:d*6);
oth = output(:,d*6+1:d*7);
sun = output (:,d*7+1:d*8);
wnd = output(:,d*8+1:d*9);
biomass = biomass';
[m,n] = size(biomass);
coal = coal';
gas = gas';
gas2 = gas2';
hydro = hydro';
nuclear = nuclear';
oth = oth';
sun = sun';
wnd = wnd';
biomass1201 = reshape (biomass,m*n,1)
coal1201 =reshape(coal,m*n,1);
gas1201 = reshape(gas,m*n,1);
gas21201 = reshape(gas2,m*n,1);
hydro1201 = reshape(hydro,m*n,1);
nuclear1201 = reshape(nuclear,m*n,1);
oth1201 = reshape(oth,m*n,1);
sun1201 = reshape(sun,m*n,1)
wnd1201 = reshape(wnd,m*n,1);
save data1201 biomass1201 coal1201 gas1201 gas21201 hydro1201 nuclear1201 oth1201 sun1201 wnd1201;

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 9일
Your code cannot work.
You have a table() object that includes a non-numeric first column and several non-numeric trailing columns (the several commas in a row will become non-numeric variables).
You delete the first (non-numeric) variable from the table... leaving the empty non-numeric ones at the end of the list.
You go through and extract various rows from the table. You use () indexing so each result is a table object.
You try to [] the extracted table objects, but that fails because the table objects all have the same variable name but you cannot have duplicate variables in the same table.
If you were to use {} indexing instead of () then that would fail because you can only extract multiple variables if they all have the same data type, but the trailing (empty) columns are a different data type.
You could get rid of trailing empty columns and then use {} indexing, or you could use vertical concatenation instead of horizontal, or you could switch to cell array or you could put each extract as a distinct variable in the [] table.
  댓글 수: 3
Kushal Bhalla
Kushal Bhalla 2021년 5월 16일 10:39
Thank you Sir for clarifying the problem. Also thanks for suggesting a way out to run the original code. Appreciate!

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Scott MacKenzie
Scott MacKenzie 2021년 5월 9일
편집: Scott MacKenzie 2021년 5월 9일
Here are three solutions. They all give nine columns, one for each source, as per your question. The first table (dataNew1) is a simple reorganization with 31 x 96 = 2976 rows. The second table (dataNew2) aggregates the data by computing the mean value over 31 days in the month. This yields 96 rows, one for each 15-minute interval. The third table (dataNew3) aggregates the data by computing the mean over the 15-minute intervals. This yields 31 rows, one for each day.
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/612335/data.csv');
sourceNames = data{1:9,1};
sourceNames = split(sourceNames, '-'); % date + source name
sourceNames = sourceNames(:,2); % just the source name
% reorganize the data (show each step in a separate variable)
d1 = data{:,2:end};
d2 = d1(:);
% organize data into 9 columns and 31x96 = 2976 rows
d3 = reshape(d2,9,[]);
d4 = d3';
dataNew1 = array2table(d4); % 2976x9
dataNew1.Properties.VariableNames = sourceNames;
% organize data into 9 columns and 96 rows
d5 = reshape(d2,9,31,96);
d6 = mean(d5,2); % compute mean over days (31)
d7 = squeeze(d6);
d8 = d7';
dataNew2 = array2table(d8); % 96x9
dataNew2.Properties.VariableNames = sourceNames;
% organize the data into 9 columns and 31 rows
d9 = reshape(d2,9,31,96);
d10 = mean(d9,3); % compute mean over 15-minute intervals (96)
d11 = d10';
dataNew3 = array2table(d11); % 31x9
dataNew3.Properties.VariableNames = sourceNames;
  댓글 수: 3
Kushal Bhalla
Kushal Bhalla 2021년 5월 16일 10:37
Sure Sir.
Thank you so much for your prompt help and assistance. Really appreciate!

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by