Reading from Excel File and
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Wal MathEngineering
le 20 Avr 2022
Réponse apportée : Voss
le 20 Avr 2022
Hello,
I am doing a review of MATLAB basics. Attached to this comment is Excel file where it shows the amount of moeny spent from 2015 to 2018.
I want to use for loop(if possible) to print in comand window the total amount spent in 2015,2016,2017,2018 ( as it shows in fprintf). And to have the total amount in all years. Excel file for more details, columns D through H for clarifications.
I have already plot the figures of each year with the money spent. It works fine. Here is my code:
%%
clc
clear
%%
% read table
data = readtable('SpentinUSDollar.xlsx')
% use year() function to get the year from a datetime array
Udate2015 = year(data{:,1}) == 2015;
% logical indexing to get a sub-table containing data from year 2015
tdate2015 = data(Udate2015,:)
% use year() function to get the year from a datetime array
Udate2015 = year(data{:,1}) == 2015;
% logical indexing to get a sub-table containing data from year 2015
tdate2015 = data(Udate2015,:)
%%
Udate2016 = year(data{:,1}) == 2016;
% logical indexing to get a sub-table containing data from year 2015
tdate2016 = data(Udate2016,:)
% use year() function to get the year from a datetime array
Udate2016 = year(data{:,1}) == 2016;
% logical indexing to get a sub-table containing data from year 2015
tdate2016 = data(Udate2016,:)
%%
Udate2017 = year(data{:,1}) == 2017;
% logical indexing to get a sub-table containing data from year 2015
tdate2017 = data(Udate2017,:)
% use year() function to get the year from a datetime array
Udate2017 = year(data{:,1}) == 2017;
% logical indexing to get a sub-table containing data from year 2015
tdate2017 = data(Udate2017,:)
%%
Udate2018 = year(data{:,1}) == 2018;
% logical indexing to get a sub-table containing data from year 2015
tdate2018 = data(Udate2018,:)
% use year() function to get the year from a datetime array
Udate2018 = year(data{:,1}) == 2018;
% logical indexing to get a sub-table containing data from year 2015
tdate2018 = data(Udate2018,:)
%% the issue is from here to fprintf
year = 0;
for i = 1:size(data,1)
if strcmp(cell2mat(data(i,4)),year)
year = year+1
end
end
fprintf(' The total spent money in %d is %s\n',data,year(data{:,1}))
fprintf(' The total of spent money of all years is %0.1f',data,total(i))
%%Anything below plot works fine.
%% plot
figure
subplot(2,2,1)
plot(tdate2015{:,1},tdate2015{:,3},'g-','Linewidth',1.8)
title('Monthly Stipend 2015')
xlabel('Date')
ylabel(' $ US Dollar')
grid on
%%
subplot(2,2,2)
plot(tdate2016{:,1},tdate2016{:,3},'b-','Linewidth',1.8)
title('Monthly Stipend 2016')
xlabel('Date')
ylabel(' $ US Dollar')
grid on
%%
subplot(2,2,3)
plot(tdate2017{:,1},tdate2017{:,3},'k-','Linewidth',1.8)
title('Monthly Stipend 2017')
xlabel('Date')
ylabel(' $ US Dollar')
grid on
%%
subplot(2,2,4)
plot(tdate2018{:,1},tdate2018{:,3},'r-','Linewidth',1.8)
title('Monthly Stipend 2018')
xlabel('Date')
ylabel(' $ US Dollar')
grid on
0 commentaires
Réponse acceptée
Voss
le 20 Avr 2022
data = readtable('SpentinUSDollar.xlsx');
% remove extra columns from table:
data(:,4:end) = [];
% get the set of relevant years:
all_years = year(data{:,1});
% get the set of unique years to loop over:
u_years = unique(all_years);
n_years = numel(u_years);
% initialize yearly totals:
total_year = zeros(n_years,1);
% loop over unique years:
for ii = 1:n_years
% total for each year:
total_year(ii) = sum(data{all_years == u_years(ii),3});
fprintf(' The total spent money in %d is $%.2f\n',u_years(ii),total_year(ii))
end
% total for all years
total_all_years = sum(data{:,3});
fprintf(' The total of spent money of all years is $%.2f',total_all_years);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!