Hello, I am using inputdlg and asking two questions.

1 view (last 30 days)
I then want to use If and Elseif fuctions from those answers. How do assign the inputs from my question to then do that fuction on them?
So far i have x = inputdlg({'Day of travel', 'Hour of travel'}, 'Peak/Off-Peak', [1 30; 1 30]);
I need to basically have a list of days of the week and hours of travel.
Thanks
Andy
  3 Comments
Geoff Hayes
Geoff Hayes on 15 Feb 2019
Andrew's answer moved here
So this is what ive got now.
x = inputdlg({'Day of travel', 'Hour of travel'}, 'Peak/Off-Peak', [1 30; 1 30])
day = x{1:1}
hour = x{2:2}
if 'Monday', day & hour >= 7 <= 10
'Peak Travel'
elseif 'Monday', day & hour >= 16 <= 18
'Peak Travel'
else
'poop'
end
but im getting Matrix dimentsions must agree. I have looked and think i need to use strcmp to avoid that. but i dont really know how to use it
Geoff Hayes
Geoff Hayes on 15 Feb 2019
Andrew - I guess the above is just pseudo-code (since not valid MATLAB syntax). To compare two strings use either strcmp or strcmpi (for case insensitive comparisons). To see if a string matches 'Monday' you would do
if strcmpi(dayOfWeek, 'Monday')
% do something
end
Note that you will probably need to convert your hour to a number in order to compare it to your 7 and 10 like
hourOfDay = str2double(x{2});
if hourOfDay >= 7 && hourOfDay <= 10
% peak travel
elseif ...
end

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 Feb 2019
The code could be more like
x = inputdlg({'Day of travel', 'Hour of travel'}, 'Peak/Off-Peak', [1 30; 1 30])
day = x{1};
hour = str2double(x{2});
isPeakTravel = false;
if strcmpi(day, 'Monday') && ((hour >= 7 && hour <= 10) || (hour >= 16 && hour <= 18))
isPeakTravel = true;
end

More Answers (2)

Andrew Mackintosh
Andrew Mackintosh on 15 Feb 2019
when i use the str2double on the hour when i put in an answer above 10, then it still tells me peak travel when it shouldnt.
this is the code i have written in matlab
x = inputdlg({'Day of travel', 'Hour of travel'}, 'Peak/Off-Peak', [1 30; 1 30])
day = x{1}
hour = str2double(x{2})
if day == 'Monday' & hour >= 7 <= 10
'Peak Travel'
elseif 'Monday', day & hour >= 16 <= 18
'Peak Travel'
else
'poop'
end
  1 Comment
Geoff Hayes
Geoff Hayes on 15 Feb 2019
Edited: Geoff Hayes on 15 Feb 2019
The above is not valid syntax for MATLAB. When comparing strings, you need to use either strcmp or strcmpi so that you do not get the "matrix dimensions must match" error. And if you want to see if a number is within a range then you need to compare that number with both the lower and upper bounds of the interval like
hour >= 16 && hour <= 18

Sign in to comment.


Andrew Mackintosh
Andrew Mackintosh on 15 Feb 2019
i dont follow what you mean? all of it? only specific bits?

Categories

Find more on Dates and Time 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!