How to search a specific row and a column from a csv file that are associated with the date that was selected?

172 views (last 30 days)
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!

Accepted Answer

Arif Hoq
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);
DateFormat = '2014-05-26';
A=input('put the date: ');
[idx C]=ismember(A,date)
output= data(C,:);
Steven Shaaya
Steven Shaaya on 9 Mar 2022
Edited: Steven Shaaya on 9 Mar 2022
Yes, but I am geting another problem now! I can not plot the data for selected rows and columns.
I get this error
" Error using tabular/plot
Too many input arguments."
I looked into the same problem online but they are not working for me.
Here is how I am trying to plot them.
data=readtable('Cooling.csv','ReadVariableNames', true)
IN=input('put the date: ');
[idx C]= ismember(IN,date)
Data_Selected= data(C:(C+4),:)
I know how to use readmatrix and hard code the specified rows and columns to plot them which it does work, but readmatrix does not work with the solution that you provide to me.
Also, I do not want to hard coded because the numbers of rows can be 1 or as large as 10000!
please note that on "Data_Selected= data(C:(C+4),:)"
for C+4 I am just trying to say plot from Row C to Row C+4 just for the purpose to see how does it work.
Can you help on this?

Sign in to comment.

More Answers (1)

Peter Perkins
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
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.




Community Treasure Hunt

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

Start Hunting!