Hello, I am using inputdlg and asking two questions.
1 view (last 30 days)
Show older comments
Andrew Mackintosh
on 15 Feb 2019
Edited: Geoff Hayes
on 15 Feb 2019
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
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
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
Accepted Answer
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
0 Comments
More Answers (2)
Andrew Mackintosh
on 15 Feb 2019
1 Comment
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
See Also
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!