How to use the 'date picker' date as input from the user?

11 vues (au cours des 30 derniers jours)
Steven Shaaya
Steven Shaaya le 10 Mar 2022
I have a csv file that have two columns one for timestamp which I want to use as x-axis and another one that I want to use as y-axis.
I wrote a script code that will ask the user for which period of time that the user wants to plot the data. The user have to enter the date as is specified by the code such as "yyyy-mm-dd". Then the code will look which row match this date. The user is asked to enter the starting date and the ending date like "2019-03-30" to "2019-12-31". The code works fine for the above description!
data=readtable('EKW.csv','ReadVariableNames', true);
Time_Stamp=datetime(data.Timestamp,'InputFormat','yyyy-MM-dd''T''HH'); % I need this to show timestamp on x-axis
Date2String = string(table2cell(data(:,1)));
RemoveTime = eraseBetween(Date2String,11,13); % to keep date as "yyyy-mm-dd" instead of "yyyy-mm-ddTHH"
Date = RemoveTime;
% Your input should be "yyyy-mm-dd" format including the quotation marks
IN1=input('Enter the starting date: '); % I need to use this for first date picker
[idx, R1]= ismember(IN1,Date);
IN2=input('Enter the ending date: '); % I need to use this for second date picker
[idx, R2]= ismember(IN2,Date);
x_axis= Time_Stamp(R1:R2,:);
x=x_axis;
y=data.YYY(R1:R2,:);
plot(x,y)
Now, I want to use this code in 'App Designer'. The way I wanted it to work is by having 'Axes' and a 'button' to upload the file and two 'date picker'. I want to use my first date picker as the first input from the user which is IN1 and the second 'date picker' as the second IN2.
Here is some of the code for the app. I also included the whole code for the app designer as well as the csv file.
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: UPLOADButton
function UPLOADButtonPushed(app, event)
data=readtable('EKW.csv','ReadVariableNames', true);
Time_Stamp=datetime(data.Timestamp,'InputFormat','yyyy-MM-dd''T''HH');
Date2String = string(table2cell(data(:,1)));
RemoveTime = eraseBetween(Date2String,11,13);
Date=RemoveTime;
% IN1=input('Enter the starting date: ');
% [idx R1]= ismember(IN1,Date);
%
% IN2=input('Enter the ending date: ');
% [idx R2]= ismember(IN2,Date);
%
% x_axis= Time_Stamp(R1:R2,:);
% x=x_axis;
% y=data.YYY(R1:R2,:);
% plot(app.UIAxes,x,y)
end
% Value changed function: StartDatePicker
function StartDatePickerValueChanged(app, event)
value1 = app.StartDatePicker.Value;
IN1=datestr(value1)
[idx R1]= ismember(IN1,Date);
end
% Value changed function: EndingDatePicker
function EndingDatePickerValueChanged(app, event)
value2 = app.EndingDatePicker.Value;
IN2=datestr(value2)
[idx R2]= ismember(IN2,Date);
x_axis= Time_Stamp(R1:R2,:);
x=x_axis;
y=data.YYY(R1:R2,:);
plot(app.UIAxes,x,y)
Okay, at this point I do not know if the logic that I am using into the app designer is correct because I am getting the following error once I select 2019-01-03 date on the first date picker. Even though I specified the date format to yyyy-mm-dd it keep changing it to dd-mmm-yyyy.
IN1 =
'03-Jan-2019'
Cannot find an exact (case-sensitive) match for 'Date'
The closest match is: date in D:\Matlab\toolbox\matlab\timefun\date.m
Error in Gui_app/StartDatePickerValueChanged (line 43)
[idx R1]= ismember(IN1,Date);
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 378)
Error while evaluating DatePicker PrivateValueChangedFcn.
Can you please help me to get what I want to work.

Réponses (1)

Benjamin Thompson
Benjamin Thompson le 10 Mar 2022
You should enclose the second argument to ismember in single quotes, as in:
[idx R1]= ismember(IN1,'Date');

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by