- 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?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Justin Tuttle
el 3 de Nov. de 2021
Comentada: Stephen23
el 3 de Nov. de 2021
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 comentario
Stephen23
el 3 de Nov. de 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'))
Respuesta aceptada
Yongjian Feng
el 3 de Nov. de 2021
Why do you need to know the size of B? Maybe you want
disp(str2double(B))
2 comentarios
Yongjian Feng
el 3 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!