I keep getting "Undefined operator '*' for input arguments of type 'cell' " when I run this code.

Carspeed=menu('Select a unit of speed','Miles/hour','Kilometers/hour','Meters/second','feet/minute');%displaying menu
iSpeed=inputdlg('How fast was the car traveling in your chosen unit of speed?');%user input for how fast the car is traveling
switch (Carspeed) %changing everything to km/h
case 1 %switching mph to km/h
fSpeed=(iSpeed*1.60934);
case 2 %km/h to km/h so no conversion in this case
fSpeed=iSpeed;
case 3 %meters/sec to km/h
fSpeed=(iSpeed*0.001)/0.0002778;
case 4 %feet/min to km/h
fSpeed=(iSpeed*0.0003048)/0.01667;
end
Cartime=menu('Select a unit of time','Seconds','Minutes','Hours','Days'); %displaying menu for time
iTime=inputdlg('How long was the car travleing for in your chosen unti of time?'); %user input for how loing the car traveled
switch (Cartime) %switch menu to change units to hours
case 1 %seconds to hours
fTime=(iTime*0.002777783);
case 2 %minutes to hours
fTime=(iTime*0.016667);
case 3 %hour to hour so no conversion
fTime=iTime;
case 4 %days to hours
fTime=(iTime*24);
end
totaldistance=fSpeed*fTime; %calculating final distance
messagebox=('The total distance traveled is');
I know this code is not completely finished however I am getting the error early on in the code. Anything helps!

回答(1 个)

inputdlg returns a cell array of character vectors, which as the error message tells you cannot be multiplied. You need to get the character vector in the first (and only) cell and convert it to numeric before you can multiply it with anything. Instead of this:
iSpeed = inputdlg(...)
you could use str2double like this:
iSpeed = str2double(inputdlg(...))

类别

在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by