34 views (last 30 days)

Show older comments

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'.

Walter Roberson
on 9 May 2021

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.

Scott MacKenzie
on 9 May 2021

Edited: Scott MacKenzie
on 9 May 2021

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;

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

Start Hunting!