'readtable' in 2023 version error (This issue did not occur in 2018)

Ele_intensity = readtable('electricity_consumption_carbon_intensity.csv');
Ele_intensity= Ele_intensity{1105:1152,2}; %Import data from row 1105 to 1152 ,column 2
Gas_intensity = readtable('gas_consumption_carbon_intensity.csv');
Gas_intensity= Gas_intensity{481:528,2}; %Import data from row 481 to 528 ,column 2
Ele_Quantity = readtable('electricity_consumption_quantity_price.csv');
Ele_Quantity= Ele_Quantity{1105:1152,2}; %Import data from row 1105 to 1152 ,column 2
Gas_Quantity = readtable('gas_consumption_quantity_price.csv');
Gas_Quantity= Gas_Quantity{481:528,2}; %Import data from row 481 to 528 ,column 2
The last four rows of code successfully import data from the csv document in both 2023b and 2018b,but the first four rows of code only import the right data in R2018b and in 2023b the array only has many 3. So I was wondering what was wrong with that,and is that code incompatible in two versions?

2 Comments

How readtable() handles titles, headers etc might have changed from R2018b to R2023b.
You should check and compare the respective documentations to see what changes have been made.
Additionally, you can ese detectImportOptions for both versions and see the output.
opts =
DelimitedTextImportOptions -
Delimiter: {'/'}
Whitespace: '\b\t '
LineEnding: {'\n' '\r' '\r\n'}
CommentStyle: {}
ConsecutiveDelimitersRule: 'split'
LeadingDelimitersRule: 'keep'
TrailingDelimitersRule: 'ignore'
EmptyLineRule: 'skip'
Encoding: 'UTF-8'
MissingRule: 'fill'
ImportErrorRule: 'fill'
ExtraColumnsRule: 'addvars'
VariableNames: {'Var1', 'Var2', 'Var3'}
VariableTypes: {'double', 'double', 'char'}
SelectedVariableNames: {'Var1', 'Var2', 'Var3'}
VariableOptions: 显示所有 3 VariableOptions
使用 setvaropts/getvaropts 访问 VariableOptions 子属性
VariableNamingRule: 'modify'
DataLines: [2 Inf]
VariableNamesLine: 0
RowNamesColumn: 0
VariableUnitsLine: 0
VariableDescriptionsLine: 0
This is the CSV document,and that shows the time in first column,and the data I need is in the second column
This is what matlab shows when import the whole document ,the ’carbon intensity‘column appear in the same column as time,and there are two additional columns that does not show up on the original csv file.
Thank you for your reply

Sign in to comment.

 Accepted Answer

I do not understand ‘2023b the array only has many 3’, however I believe that the shortened table display in the last few releases is simply a display issue. The entire file is being imported correctly.

6 Comments

This is the CSV document,and that shows the time in first column,and the data I need is in the second column
This is what matlab shows when import the whole document ,the ’carbon intensity‘column appear in the same column as time,and there are two additional columns that does not show up on the original csv file.
Thank you for your reply
It would be best for you to upload the file. Use the ‘paperclip’ button (just to the right of the Σ button) on the top toolstrip.
I changed the table names for convenience (making it easier to import them in a loop), otherwise your code is unchanged.
Try this —
% type('GroupNumber36_Carbon.m')
files = dir('*.csv');
for k = 1:numel(files)
filename = files(k).name
T{k,:} = readtable(filename, 'VariableNamingRule','preserve');
end
filename = 'electricity_co...intensity.csv'
filename = 'electricity_co...ity_price.csv'
filename = 'gas_consumptio...intensity.csv'
filename = 'gas_consumptio...ity_price.csv'
T{:}
ans = 15888x2 table
Period_UTC Carbon Intensity (g/kWh) ________________ ________________________ 01/03/2023 00:00 218 01/03/2023 00:30 220 01/03/2023 01:00 222 01/03/2023 01:30 224 01/03/2023 02:00 224 01/03/2023 02:30 220 01/03/2023 03:00 218 01/03/2023 03:30 219 01/03/2023 04:00 221 01/03/2023 04:30 219 01/03/2023 05:00 216 01/03/2023 05:30 217 01/03/2023 06:00 221 01/03/2023 06:30 233 01/03/2023 07:00 235 01/03/2023 07:30 235
ans = 15888x3 table
Period_UTC Quantity (kwh) Price (p/kwh incl VAT) ________________ ______________ ______________________ 2023-03-01 00:00 0.102 24.938 2023-03-01 00:30 0.1 25.578 2023-03-01 01:00 0.1 25.798 2023-03-01 01:30 0.101 24.917 2023-03-01 02:00 0.102 24.896 2023-03-01 02:30 0.099 24.539 2023-03-01 03:00 0.099 24.896 2023-03-01 03:30 0.138 23.153 2023-03-01 04:00 0.134 26.019 2023-03-01 04:30 0.131 24.801 2023-03-01 05:00 0.094 28.004 2023-03-01 05:30 0.102 28.36 2023-03-01 06:00 0.112 29.001 2023-03-01 06:30 0.135 32.193 2023-03-01 07:00 0.345 32.13 2023-03-01 07:30 0.599 33.763
ans = 15264x2 table
Period_UTC Carbon Intensity (g/kWh) ________________ ________________________ 14/03/2023 00:00 82 14/03/2023 00:30 85 14/03/2023 01:00 81 14/03/2023 01:30 71 14/03/2023 02:00 70 14/03/2023 02:30 74 14/03/2023 03:00 77 14/03/2023 03:30 78 14/03/2023 04:00 82 14/03/2023 04:30 84 14/03/2023 05:00 83 14/03/2023 05:30 86 14/03/2023 06:00 93 14/03/2023 06:30 100 14/03/2023 07:00 107 14/03/2023 07:30 119
ans = 15264x3 table
Period_UTC Quantity (kwh) Price (p/kwh incl VAT) ________________ ______________ ______________________ 2023-03-14 00:00 0 7.37 2023-03-14 00:30 0 7.37 2023-03-14 01:00 0 7.37 2023-03-14 01:30 0 7.37 2023-03-14 02:00 0 7.37 2023-03-14 02:30 0 7.37 2023-03-14 03:00 0.141 7.37 2023-03-14 03:30 1.021 7.37 2023-03-14 04:00 0.613 7.37 2023-03-14 04:30 0.514 7.37 2023-03-14 05:00 0 7.37 2023-03-14 05:30 0 7.37 2023-03-14 06:00 0 7.37 2023-03-14 06:30 0 7.37 2023-03-14 07:00 0 7.37 2023-03-14 07:30 0.842 7.37
% T{1}{:,1}.Format
% clear all
% clc
% Ele_intensity = readtable('electricity_consumption_carbon_intensity.csv');
Ele_intensity = T{1};
Ele_intensity= Ele_intensity{1105:1152,2};
% Gas_intensity = readtable('gas_consumption_carbon_intensity.csv');
Gas_intensity = T{3};
Gas_intensity= Gas_intensity{481:528,2};
% Ele_Quantity = readtable('electricity_consumption_quantity_price.csv');
Ele_Quantity = T{2};
Ele_Quantity= Ele_Quantity{1105:1152,2};
% Gas_Quantity = readtable('gas_consumption_quantity_price.csv');
Gas_Quantity = T{4};
Gas_Quantity= Gas_Quantity{481:528,2};%Import data on 24/03/2023
Total_gas_Demand=sum(Gas_Quantity);
Total_ele_Demand=sum(Ele_Quantity);
x = optimvar('x', 48, 1, 'Type', 'continuous', 'LowerBound', 0, 'UpperBound', 4);
y = optimvar('y', 48, 1, 'Type', 'continuous', 'LowerBound', 0, 'UpperBound', 1.3);
prob = optimproblem('ObjectiveSense', 'minimize');
prob.Objective = sum(Ele_intensity.* x)+sum(Gas_intensity.* y);
prob.Constraints.ele_constraint = sum(x) == sum(Total_ele_Demand);
prob.Constraints.gas_constraint = sum(y) == sum(Total_gas_Demand);
[sol,fval] = solve(prob);
Solving problem using linprog. Optimal solution found.
% optimised_Ele_Quantity = getfield(sol,'x');
% optimised_Gas_Quantity = getfield(sol,'y');
optimised_Ele_Quantity = sol.x;
optimised_Gas_Quantity = sol.y;
optimised_emit_Ele=sum(Ele_intensity.*optimised_Ele_Quantity);
optimised_emit_Gas=sum(Gas_intensity.*optimised_Gas_Quantity);
Ele_Saving=sum(Ele_intensity.*Ele_Quantity)-optimised_emit_Ele;
Gas_Saving=sum(Gas_intensity.*Gas_Quantity)-optimised_emit_Gas;
fprintf('Optimised total emission: %f\n', fval);
Optimised total emission: 842.372000
fprintf('Ele emit : %f g\n',optimised_emit_Ele);
Ele emit : 478.415000 g
fprintf('Gas emit : %f g\n',optimised_emit_Gas);
Gas emit : 363.957000 g
fprintf('Ele saving : %f g\n',Ele_Saving);
Ele saving : 151.073000 g
fprintf('Gas saving : %f g\n',Gas_Saving);
Gas saving : 75.661000 g
non_optimised_total_emit=sum(Ele_intensity.*Ele_Quantity)+sum(Gas_intensity.*Gas_Quantity);
total_saving=non_optimised_total_emit-fval;
fprintf('total saving : %f g\n',total_saving);
total saving : 226.734000 g
This is working correctly in R2024a, however I believe R2023b would have produced the same relsult.
Are there problems?
.
Thank you!!!! It’s working!!Thank you for your help!
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Asked:

Si
on 23 Mar 2024

Edited:

on 23 Mar 2024

Community Treasure Hunt

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

Start Hunting!