Hello, I am using inputdlg and asking two questions.

2 vues (au cours des 30 derniers jours)
Andrew Mackintosh
Andrew Mackintosh le 15 Fév 2019
Modifié(e) : Geoff Hayes le 15 Fév 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 commentaires
Geoff Hayes
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
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

Connectez-vous pour commenter.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 15 Fév 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

Plus de réponses (2)

Andrew Mackintosh
Andrew Mackintosh le 15 Fév 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 commentaire
Geoff Hayes
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

Connectez-vous pour commenter.


Andrew Mackintosh
Andrew Mackintosh le 15 Fév 2019
i dont follow what you mean? all of it? only specific bits?

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by