How to search a specific row and a column from a csv file that are associated with the date that was selected?
8 views (last 30 days)
Show older comments
How to search a specific row(s) and a column(s) from a csv file that is/are associated with the date that was selected and display which row(s) has this date.
I have 356 rows and the column #1 has data that start with 01/01/2022 up to 12/31/2022.
Now, if a user enter 01/27/2022 than I want to search for which row on the first column has a date that match the selected date once this row is found then I know column #2 has data for x-axis and column #3 has data for y-axis. So once I found this row(s) then I can plot the data that are on column #2 and #3 for the row(s) that was found.
The reason for me saying "rows" is because I have about 9000 rows of data and that I have over more than one row for each day!
0 Comments
Accepted Answer
Arif Hoq
on 9 Mar 2022
try this. please follow the dateformat whenever user put the date. I have attached a random csv file.
data=readtable('VIX.csv','ReadVariableNames', false);
date=string(table2cell(data(:,1)));
DateFormat = '2014-05-26';
A=input('put the date: ');
[idx C]=ismember(A,date)
output= data(C,:);
5 Comments
More Answers (1)
Peter Perkins
on 9 Mar 2022
It's a LOT easier using a timetable:
>> x = (1:20)';
>> y = rand(size(x));
>> date = datetime(2022,1,repelem(1:4,5)');
>> tt = timetable(date,x,y)
tt =
20×2 timetable
date x y
___________ __ ________
01-Jan-2022 1 0.072686
01-Jan-2022 2 0.63163
01-Jan-2022 3 0.88471
01-Jan-2022 4 0.27271
01-Jan-2022 5 0.43641
[snip]
04-Jan-2022 16 0.90465
04-Jan-2022 17 0.50452
04-Jan-2022 18 0.51629
04-Jan-2022 19 0.31903
04-Jan-2022 20 0.98664
>> tt("2022-01-03",:)
ans =
5×2 timetable
date x y
___________ __ ________
03-Jan-2022 11 0.46445
03-Jan-2022 12 0.94098
03-Jan-2022 13 0.050084
03-Jan-2022 14 0.76151
03-Jan-2022 15 0.7702
>> plot(tt.x('2022-01-03'),tt.y('2022-01-03'))
If you are doing this a lot, look at using rowfun with date as the grouping variable.
You are getting your data from a file. If your version of MATLAB is too old to have readtimetable, use readtable and table2timetable.
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!