- when you specify the 's' option with INPUT then you have requested its outout to be returned as a character vector: this means if the user inputs five character, you will get a character vector with length five.
- when you write those five character at the command line then they are evaluated, e.g. into a single numeric value.
How can I force MATLAB to arrange inputs into a matrix of specified dimensions?
8 次查看(过去 30 天)
显示 更早的评论
I am currently teaching my self MATLAB. I made a simple program to practice what I have leaned so far but ran into an issue with the input command. Any time I enter a value with more than two characters MATLAB constructs a matrix with each character in a different column (for example 123 yeilds the matrix [1,2,3]). I'm confued because If I run the input command in the command window this doesn't occur. How can I ensure that the input command yeilds a matrix of the correct size ?
%test is a function which calculates the relativistic energy of a particle
% with specified speed (in units of c) and mass (in kg)
help test;
%constant
C=3e8;
%defining variables
B=input('enter the speed of the particle in units of c','s');
disp (size (B))
1 个评论
Stephen23
2021-11-3
You are mixing up numeric data with character data:
These are two totally different things.
B = str2double(input('enter the speed of the particle in units of c','s'))
采纳的回答
Yongjian Feng
2021-11-3
Why do you need to know the size of B? Maybe you want
disp(str2double(B))
2 个评论
Yongjian Feng
2021-11-3
B is an array of char like '123', not a double 123.
str2double converts the input char array '123' into a double of 123.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!