I would like to store each iteration of a for loop into a row vector, in order. This particular problem does not start with a vector, but instead with separate scalar user inputs that must be placed into a vector. How do I achieve this? Thanks!

3 次查看(过去 30 天)
Random variables seem to be placed into a row vector instead. I've been trying for hours and can't figure this out.
num=input('Enter the number of data points: ');
decimal=input('Enter the number of decimals you want to show: ');
for c=1:1:num
dp=input(sprintf('Data Point #%0.0f: ',c));
Vector=dp(1,c)
output(c)=Vector
end

采纳的回答

Star Strider
Star Strider 2017-3-4
I prefer the inputdlg function to input. so I use it here. Change it back if you like. (I do not recommend it, though!)
This code seems to do what you want
numc=inputdlg('Enter the number of data points: ');
num = str2num(numc{:});
decimalc=inputdlg('Enter the number of decimals you want to show: ');
decimal = str2num(decimalc{:});
for c=1:1:num
dpc=inputdlg(sprintf('Data Point #%0.0f: ',c));
dp = str2num(dpc{:});
Vector(c)=dp;
output(c)=Vector(c)
end
  3 个评论
Stephen23
Stephen23 2017-3-4
编辑:Stephen23 2017-3-4
inputdlg returns strings, which must be converted to numeric. That is why str2num is used. Even better would be to use str2double, which is more robust, faster, and gives predictable output when the input is unexpected.
Star Strider
Star Strider 2017-3-4
@Jenel Darland — My pleasure! I very much appreciate your compeliment! I prefer inputdlg because it doesn’t clutter my Command Window. It’s also more robust, since it can accept strings and numbers in one call. They have to be parsed subsequently, but this is easy.
@Stephen — The str2double function didn’t always work with arrays (it does now), so I got used to using str2num. Your observations on str2double are correct, and noted.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by