- 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 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Justin Tuttle
il 3 Nov 2021
Commentato: Stephen23
il 3 Nov 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 Commento
Stephen23
il 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'))
Risposta accettata
Yongjian Feng
il 3 Nov 2021
Why do you need to know the size of B? Maybe you want
disp(str2double(B))
2 Commenti
Yongjian Feng
il 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!