Effacer les filtres
Effacer les filtres

logical index plot specific columns

1 vue (au cours des 30 derniers jours)
Wal MathEngineering
Wal MathEngineering le 19 Avr 2022
Commenté : Voss le 20 Avr 2022
Hi,
I made excel file with date and US dollar spent from 2015 to 2018. I graphed the two columns in excel but I also want to apply same knowledge in MATLAB using logical index. I want to use logical index using string compare to make a new table with each year corresponds with the amount of money spent in that specific year.
here is my first try but not sure why logical index doesn't work.
%%
clc
clear
%%
% read table
data = readtable('SpentinUSDollar.xlsx')
Udate = strcmp(data(:,1),'2015');
tdate = data(Udate,:);
does this work to use logical index? I was thinking to use for loop and the size of the table. Then to make MATLAB to look for 2015 and create table between this year and US Dollar. See the file for more details.

Réponse acceptée

Voss
Voss le 20 Avr 2022
%%
clc
clear
%%
% read table
data = readtable('SpentinUSDollar.xlsx','NumHeaderLines',1)
data = 51×4 table
Var1 Var2 Var3 Var4 ___________ ____ ______ ______ 23-Aug-2015 5000 1332.5 NaN 09-Sep-2015 1000 266.51 NaN 16-Sep-2015 6000 1599.1 3.7522 27-Sep-2015 3000 799.53 NaN 01-Oct-2015 3000 799.53 NaN 08-Oct-2015 1000 266.51 NaN 18-Oct-2015 6000 1599.1 NaN 25-Oct-2015 1000 266.51 NaN 01-Nov-2015 3000 799.53 NaN 24-Nov-2015 3000 799.53 NaN 09-Dec-2015 3000 799.53 NaN 10-Jan-2016 1500 399.76 NaN 13-Jan-2016 2000 533.02 NaN 18-Jan-2016 3500 932.78 NaN 31-Jan-2016 1500 399.76 NaN 21-Feb-2016 3500 932.78 NaN
% use curly braces { } to access data from a table;
% parentheses ( ) give you a sub-table
class(data{:,1})
ans = 'datetime'
% use year() function to get the year from a datetime array
Udate = year(data{:,1}) == 2015;
% logical indexing to get a sub-table containing data from year 2015
tdate = data(Udate,:)
tdate = 11×4 table
Var1 Var2 Var3 Var4 ___________ ____ ______ ______ 23-Aug-2015 5000 1332.5 NaN 09-Sep-2015 1000 266.51 NaN 16-Sep-2015 6000 1599.1 3.7522 27-Sep-2015 3000 799.53 NaN 01-Oct-2015 3000 799.53 NaN 08-Oct-2015 1000 266.51 NaN 18-Oct-2015 6000 1599.1 NaN 25-Oct-2015 1000 266.51 NaN 01-Nov-2015 3000 799.53 NaN 24-Nov-2015 3000 799.53 NaN 09-Dec-2015 3000 799.53 NaN
  2 commentaires
Wal MathEngineering
Wal MathEngineering le 20 Avr 2022
Modifié(e) : Wal MathEngineering le 20 Avr 2022
Thanks
3 questions if I may ask
But what does 'NumHeaderLines' do ?
why we used class(data{:,1})?
Lastly why 'year' in (date{:,1})==2015?
Thanks a lot
Voss
Voss le 20 Avr 2022
NumHeaderLines tells readtable how many lines there are in the file above where the data starts. Without setting that to 1, readtable returns this:
data = readtable('SpentinUSDollar.xlsx')
data = 49×4 table
Var1 Var2 Var3 Var4 ___________ ____ ______ ______ 16-Sep-2015 6000 1599.1 3.7522 27-Sep-2015 3000 799.53 NaN 01-Oct-2015 3000 799.53 NaN 08-Oct-2015 1000 266.51 NaN 18-Oct-2015 6000 1599.1 NaN 25-Oct-2015 1000 266.51 NaN 01-Nov-2015 3000 799.53 NaN 24-Nov-2015 3000 799.53 NaN 09-Dec-2015 3000 799.53 NaN 10-Jan-2016 1500 399.76 NaN 13-Jan-2016 2000 533.02 NaN 18-Jan-2016 3500 932.78 NaN 31-Jan-2016 1500 399.76 NaN 21-Feb-2016 3500 932.78 NaN 13-Mar-2016 3500 932.78 NaN 01-May-2016 3500 932.78 NaN
Note that starts with 16-Sep-2015, skipping the first two lines of data from the file.
Using class is unnecessary. I put it there to check the class of the first column of data in the table, the dates. I left it there so you could see the first column is of class datetime.
The year function returns the year of each element of a datetime array:
year(data{:,1})
ans = 49×1
2015 2015 2015 2015 2015 2015 2015 2015 2015 2016
dt = datetime('2022-04-20')
dt = datetime
20-Apr-2022
year(dt)
ans = 2022

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by