Using nargin for a menu function

2 views (last 30 days)
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
Kyle Reagan
Kyle Reagan on 24 Jul 2017
Edited: Kyle Reagan on 24 Jul 2017
The function itself is quite long, but the part corresponding to the menu is within a while true argument as seen below. Let me know if you need more information about it. Note that once choice 4 is chosen, the MyCO2plotter continues the script and plots data (code not seen below).
function MyCO2plotter
% 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)
% Set start day to 2 weeks back if no date is chosen.
if nargin < 1
Start_Date1 = datetime('now') - 14;
Start_Date1 = dateshift(Start_Date1,'start','day');
day1 = datenum(Start_Date1);
fprintf('Start Date: %s \n', Start_Date)
end
case 2
% Case for entering end date
day2 = uigetdate;
day2 = floor(day2);
End_Date = datestr(day2);
End_Date1 = datetime(End_Date);
fprintf('End Date: %s \n', End_Date)
if day2 < day1
continueprogram = false;
clear
clc
fprintf('Error. The end date must be after the start date. Restart program and enter dates correctly. \n')
end
% Set end day to tomorrow if no date is chosen.
if nargin < 1
End_Date1 = datetime('now') + 1;
End_Date1 = dateshift(End_Date1,'start','day');
day2 = datenum(End_Date1);
fprintf('End Date: %s \n', End_Date)
end
case 3
% Cancels menu feature
continueprogram = false;
clearvars -global;
clearvars;
clc;
case 4 % Plots data
continueprogram = false;
end
end
Adam
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.

Sign in to comment.

Accepted Answer

Walter Roberson
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
  1 Comment
Kyle Reagan
Kyle Reagan on 24 Jul 2017
Thanks Walter, I was actually just about to comment that I did this (instead of messing with nargin) and saw that you had suggested the same thing. Works like a charm, thanks to all for the help.

Sign in to comment.

More Answers (0)

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!