Hello, I am using inputdlg and asking two questions.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2019-2-15
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
2019-2-15
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
采纳的回答
Geoff Hayes
2019-2-15
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 个评论
更多回答(2 个)
Andrew Mackintosh
2019-2-15
1 个评论
Geoff Hayes
2019-2-15
编辑:Geoff Hayes
2019-2-15
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!