Using nargin for a menu function
2 views (last 30 days)
Show older comments
Kyle Reagan
on 24 Jul 2017
Commented: Kyle Reagan
on 24 Jul 2017
I have a menu function with 4 choices. The first two are buttons to be pressed to enter a start date (choice 1) and an end date (choice 2). Choice 3 cancels the script (it is in a while loop), and choice 4 continues with the dates selected to plot some functions. I want to add code that says "if a start date wasn't chosen, Start_Date1 = datetime('now') - 14" and "if an end date wasn't chosen then End_Date1 = datetime('now') + 1". I tried to do this with the following code for choice 1 of the menu. However, it doesn't work when I run the program and don't select a start or end date manually. When I run it in debug mode, by executing each line at a time in the command window it works... Does anybody know what the issue is? (Note: the code is the same for choice 2 on the menu, except with End_Date1.)
if nargin < 1
Start_Date1 = datetime('now') - 14;
Start_Date1 = dateshift(Start_Date1,'start','day');
day1 = datenum(Start_Date1);
end
4 Comments
Adam
on 24 Jul 2017
Edited: Adam
on 24 Jul 2017
nargin is for arguments. Your code doesn't appear to be using a function where arguments are passed in.
I assume uigetdate returns [] or 0 or something like that if it is closed without a date selection which I assume is what you mean by no date selected. So just use that in a test.
The location of your code is confusing though because you have 4 lines of code that assume a date was chosen before you then try to handle the case of a date not being chosen, unless I am reading your code incorrectly.
Accepted Answer
Walter Roberson
on 24 Jul 2017
function MyCO2plotter
% Initialize start day to 2 weeks back
Start_Date1 = datetime('now') - 14;
Start_Date1 = dateshift(Start_Date1,'start','day');
day1 = datenum(Start_Date1);
fprintf('Start Date: %s \n', Start_Date)
% Initialize end day to tomorrow
End_Date1 = datetime('now') + 1;
End_Date1 = dateshift(End_Date1,'start','day');
day2 = datenum(End_Date1);
fprintf('End Date: %s \n', End_Date)
% First Menu
continueprogram = true;
while continueprogram
choice = menu('CO2 Plotting Tool','Enter Start Date','Enter End Date','Cancel','Plot Data');
switch choice
case 1
% Case for entering start date
day1 = uigetdate;
day1 = floor(day1); % This is becuase uigetdate starts at the hour and minute of the day you use it on
Start_Date = datestr(day1);
Start_Date1 = datetime(Start_Date); % Changes from character to variable
fprintf('Start Date: %s \n', Start_Date)
case 2
% Case for entering end date
t_day2 = uigetdate;
t_day2 = floor(t_day2);
t_End_Date = datestr(t_day2);
t_End_Date1 = datetime(End_Date);
if t_day2 < day1
fprintf('Error. The end date must be after the start date. Try again.\n');
else
day2 = t_day2;
End_Date = t_End_Date;
t_End_Date1 = t_End_Date1;
fprintf('End Date: %s \n', End_Date)
end
case 3
% Cancels menu feature
continueprogram = false;
case 4 % Plots data
%you probably want to plot at this point
continueprogram = false;
end
end
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!