Plot data Excel sheet

조회 수: 2 (최근 30일)
AHMED FAKHRI
AHMED FAKHRI 2021년 4월 9일
댓글: Soumya Paliwal 2021년 4월 9일
Hi
I have the attached Excel sheet, and I want to plot the years from 2022 to 2050 on the x-axis.
On the y-axis, I want to plot the cumulative emissions for each year above.For instance, the code should add any emission generated for the same year such that the cumulative is avaialble for each year.
Can you help please? thanks

채택된 답변

Soumya Paliwal
Soumya Paliwal 2021년 4월 9일
The following script should help you with the problem:
% Read from the excel file
rawData = readmatrix('data.xlsx');
% Find unique years
uniqueYears = unique(rawData(:,1));
cumulativeValues = zeros(length(uniqueYears),1);
mapping = containers.Map(uniqueYears,cumulativeValues);
% Find cumulative eemissions for each year
for i=1:length(rawData)
mapping(rawData(i,1)) = mapping(rawData(i,1))+rawData(i,2);
end
plot_uniqueYear = mapping.keys;
plot_cumulativeValues = mapping.values;
bar(cell2mat(plot_uniqueYear),cell2mat(plot_cumulativeValues));
xlabel('Years');
ylabel('CUmulative Emissions');
  댓글 수: 2
AHMED FAKHRI
AHMED FAKHRI 2021년 4월 9일
Many thanks that works, thanks for your help.
One last thing please, I need to add the value of the previous year always with the next so they become linear
So 2022 in x axis, then 2022+2023 in the (2023) year x-axis, and so on.
Similar to the below part in this image shaded in 'yellow'
Soumya Paliwal
Soumya Paliwal 2021년 4월 9일
You can add a small code snippet for this. Please refer to the following script:
rawData = readmatrix('data.xlsx');
uniqueYears = unique(rawData(:,1));
cumulativeValues = zeros(length(uniqueYears),1);
mapping = containers.Map(uniqueYears,cumulativeValues);
for i=1:length(rawData)
mapping(rawData(i,1)) = mapping(rawData(i,1))+rawData(i,2);
end
plot_uniqueYear = mapping.keys;
%%%%% new code snippet %%%%%
for i=2:length(plot_uniqueYear)
mapping(plot_uniqueYear{i}) = mapping(plot_uniqueYear{i}) + mapping(plot_uniqueYear{i-1});
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot_cumulativeValues = mapping.values;
bar(cell2mat(plot_uniqueYear),cell2mat(plot_cumulativeValues));
xlabel('Years');
ylabel('Cumulative Emissions');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by