Hello, I am using inputdlg and asking two questions.
Afficher commentaires plus anciens
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 commentaires
Geoff Hayes
le 15 Fév 2019
Andrew - from your code, you can extract the day of travel and the hour of travel as
x = inputdlg({'Day of travel', 'Hour of travel'}, 'Peak/Off-Peak', [1 30; 1 30]);
dayOfTravel = str2double(x{1}); % assumes integer input of 1-7
hourOfTravel = str2double(x{2}); % assumes integer input of 1-24
Then you can pass these two values into your function for further processing..using if/elseif or switch statements as needed.
Geoff Hayes
le 15 Fév 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
le 15 Fév 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
Réponse acceptée
Plus de réponses (2)
Andrew Mackintosh
le 15 Fév 2019
0 votes
1 commentaire
Geoff Hayes
le 15 Fév 2019
Modifié(e) : Geoff Hayes
le 15 Fév 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
Andrew Mackintosh
le 15 Fév 2019
0 votes
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!