Is it possible to set a vector as an users input'

1 回表示 (過去 30 日間)
Pietro Fiondella
Pietro Fiondella 2022 年 7 月 18 日
編集済み: Jan 2022 年 7 月 18 日
I want to obtain a vector as an input whit some value for each month of the year. So i did this way
while true %January [kwh]
TECJan=input("Feb [kwh] ");
if isnumeric(TECJan)
break
end
end
while true %February [kwh]
TECFeb=input("Feb [kwh] ");
if isnumeric(TECFeb)
break
end
end
while true %March [kwh]
TECMar=input("Mar [kwh] ");
if isnumeric(TECMar)
break
end
end
while true %April [kwh]
TECApr=input("Apr [kwh] ");
if isnumeric(TECApr)
break
end
end
while true %May [kwh]
TECMay=input("May [kwh] ");
if isnumeric(TECMay)
break
end
end
while true %June [kwh]
TECJun=input("Jun [kwh] ");
if isnumeric(TECJun)
break
end
end
while true %July [kwh]
TECJul=input("Jul [kwh] ");
if isnumeric(TECJul)
break
end
end
while true %August [kwh]
TECAug=input("Aug [kwh] ");
if isnumeric(TECAug)
break
end
end
while true %September [kwh]
TECSep=input("Set [kwh] ");
if isnumeric(TECSep)
break
end
end
while true %October [kwh]
TECOct=input("Oct [kwh] ");
if isnumeric(TECOct)
break
end
end
while true %November [kwh]
TECNov=input("Nov [kwh] ");
if isnumeric(TECNov)
break
end
end
while true %December [kWh]
TECDec=input("Dec [kwh] ");
if isnumeric(TECDec)
break
end
end
TEC_Mm=[TECJan,TECFeb,TECMar,TECApr,TECMay,TECJun,TECJul,TECAug,TECSep,TECOct,TECNov,TECDec];
It works for me, but i was wondering if ther is an easy way , for example ask directly for vector elements as inputs, whit the possibility to ask only one time the whole elements of TEC_Mm

採用された回答

Jan
Jan 2022 年 7 月 18 日
編集済み: Jan 2022 年 7 月 18 日
Such inputs are obtained using a GUI usually. The callbacks of the edit fields control the type of the input.
A loop would be easier also:
TEC_Mm = zeros(1, 12);
Name = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
for k = 1:12
while true
tmp = input(Name(k) + " [kWh] ");
if isnumeric(tmp) && isscalr(tmp) && isfinite(tmp)
TEC_Mm(k) = tmp;
break
else
fprintf(2, 'Bad input. Please repeat:\n');
end
end
end
Alternative:
while 1
fprintf('Please input 12 values separated by spaces in kWh');
tmp = input(" ", "s");
TEC_Mm = reshape(sscanf(tmp, "%g "), 1, []);
if isnumeric(TEC_Mm) & isequal(size(TEC_Mm), [1, 12]) & all(isfinite(TEC_Mm))
break
else
fprintf(2, 'Bad input. Please repeat:\n');
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFrequently-used Algorithms についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by