- 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?
6 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 3 Nov 2021
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'))
Accepted Answer
Yongjian Feng
on 3 Nov 2021
Why do you need to know the size of B? Maybe you want
disp(str2double(B))
2 Comments
Yongjian Feng
on 3 Nov 2021
B is an array of char like '123', not a double 123.
str2double converts the input char array '123' into a double of 123.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!