Azzera filtri
Azzera filtri

How can I force MATLAB to arrange inputs into a matrix of specified dimensions?

5 visualizzazioni (ultimi 30 giorni)
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
Stephen23 il 3 Nov 2021
You are mixing up numeric data with character data:
  • 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.
These are two totally different things.
B = str2double(input('enter the speed of the particle in units of c','s'))

Accedi per commentare.

Risposta accettata

Yongjian Feng
Yongjian Feng il 3 Nov 2021
Why do you need to know the size of B? Maybe you want
disp(str2double(B))
  2 Commenti
Justin Tuttle
Justin Tuttle il 3 Nov 2021
I just used size(B) to diagnose an "incorect dimensions for matrix multiplication" error later in my program that was a result of the matrix being produced by the input function. The same problem is occuring when defining M as well. here's my full program.
%test is a function which calculates the relativistic energy of a partic
% with specified velocity (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');
M=input('enter the mass of the particle in kg',"s");
%evaluating energy, and gamma factor
Y=1./sqrt(1-(B.^2));
E=Y*M*C^2;
disp (E)
%displaying gamma if desired
no=0;
yes=1;
Q=input ('would you like the value of gamma (enter yes or no)',"s");
if Q==1
disp (Y)
end
%displaying momentum if desired
F=input ('would you like the momentum of the particle (enter yes or no)','s');
if F==1
P=Y*M*B*C;
disp (P)
end
Yongjian Feng
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by