>> help input
input Prompt for user input.
R = input('How many apples') gives the user the prompt in the
text string and then waits for input from the keyboard.
The input can be any MATLAB expression, which is evaluated,
using the variables in the current workspace, and the result
returned in R. If the user presses the return key without
entering anything, input returns an empty matrix.
R = input('What is your name','s') gives the prompt in the text
string and waits for character string input. The typed input
is not evaluated; the characters are simply returned as a
MATLAB string.
The text string for the prompt may contain one or more '\n'.
The '\n' means skip to the beginning of the next line. This
allows the prompt string to span several lines. To output
just a '\' use '\\'.
The student version is only $99 -- just enrol in a PhD or something (or if you have kids that are students they can get it).Wish I could afford to buy MATLAB.
/ sum_of_two_2x3_matrices.cpp
// read two 2x3 matrices and find sum
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
const int R = 2, C = 3;
int r, c;
float m1[R][C];
float m2[R][C];
float sum_m3[R][C];
float sum = 0;
cout << "Enter matrix #1 below\n\n";
for ( r=0; r<R; r++)
{
for (c=0; c<C; c++)
{
cout << "enter entry for row #" << (r+1) << " and column"
<< " #" << (c+1) << ": ";
cin >> m1[r][c];
}
}
cout << "\n\nEnter matrix #2 below\n\n";
for ( r=0; r<R; r++)
{
for (c=0; c<C; c++)
{
cout << "enter entry for row #" << (r+1) << " and column"
<< " #" << (c+1) << ": ";
cin >> m2[r][c];
}
}
for ( r=0; r<R; r++ )
{
for ( c=0; c<C; c++)
{
sum_m3[r][c] = m1[r][c] + m2[r][c];
}
}
cout << "\n Sum of matrix #1 and matrix #2 is given below\n\n";
cout << setw(10) << "" << setw(10) << "col 1" << setw(10) << "col 2" << setw(10)
<< "col 3\n";
for ( r=0; r<R; r++ )
{
cout << "\n" << setw(9) << "row " << r+1;
for ( c=0; c<C; c++ )
{
cout << setw(10) << sum_m3[r][c];
}
}
cout << "\n\n";
system("pause");
return 0;
}
A = input('Please enter matrix A: ')
B = input('Please enter matrix B: ')
fprintf('A + B = \n\n');
sum = A + B;
disp(sum);
Please enter matrix A: [1 2 3; 1 1 1]
A =
1 2 3
1 1 1
Please enter matrix B: [2 2 2; 4 5 6]
B =
2 2 2
4 5 6
A + B =
3 4 5
5 6 7
fprintf('Please enter matrix A: ');
A = input();
fprintf('Please enter matrix A: ');
B = input();
fprintf('\n\n\''Hello\n\n');
fprintf('A + B = \n\n');
sum = A + B;
disp(sum);
Please enter matrix A: Error using input
Not enough input arguments.
Error in matrix_addition2 (line 2)
A = input();
vlad777 said:fprintf is more powerful than disp and lets you combine strings with variable values in screen output,
in any way you want.
dougy83 said:The disp function displays the matrix passed to it after formatting it. e.g. you can use it to display all of the elements of A using disp(A). You can also display strings, e.g. disp(['First element of A is ' A(1,1)]
% the code below is used to add any two matrices
fprintf('Please enter matrix A: \n');
A = input('')
fprintf('Please enter matrix B: \n');
B = input('')
fprintf('\n\nHello\n\n');
fprintf('A + B = \n\n');
sum = A + B;
fprintf(['A + B = \n\n'sum); % error
disp(['A + B = \n\n'A(1,1)]); % error
% the following program is used to draw graphs of three basic trignometric
% functions
clear
clc
A=1;
ph=0;
fprintf('if a problem occurs while execution please enter CTRL+C for termination\n\n\n');
choice = input('which trig. function you want to draw, enter "s" for sin , eneter "c" for cos, and for tan enter "t": ' );
if (choice==s)
fprintf('the sine function will be drawn using this equation, A.*sin(x+ph)\n');
drawFunction = A.*sin(x+ph);
fprintf('please enter the range for the graph\n');
min = input('enter lower range value: ');
max = input('enter maximum value for your range: ');
range = [min:0.01:max];
default1=input('do you want to change default values, "1" and "0" for "A" and "ph" respectively? If no, then press "n", otherwise, "y"\n: ');
if (default1==y)
A = input('enter value for "A": ');
ph = input('enter value for phase angle: ');
else
plot(x,drawFunction,'r','-.'),xlabel('x'),ylabel('y'),legend('sin(x)')
end
end
which trig. function you want to draw, enter "s" for sin , eneter "c" for cos, and for tan enter "t": s
Error using input
Undefined function or variable 's'.
Error in main_trigno_functions (line 12)
choice = input('which trig. function you want to draw, enter "s" for sin , eneter "c" for cos, and for tan enter "t": ' );
I didn't think printf could be used to display matrices natively. You'd have to iterate through the elements. Use disp(A) instead.Could you please tell me how to combine string and variable values using fprintf? You can use the code given below. Thanks.
Sorry, my mistake. Use disp(['here is A(1,1): ' num2str(A(1,1))]);It's not working for me. Where am I going wrong? Please let me know. Thanks.
Please read post 2 and post 15 above. Type "help input" into the matlab command window to get details on how to use the input function. You tried to use the value of the 's' variable, which doesn't exist. Try using input to read and return a string value rather than an expression (see help for how this is done).Could you please tell me what I did wrong? When I press "s" I get an error which is given below.
x=[0:50];
y=3;
stem(x,y);
??? Error using ==> stem at 44
X must be same length as Y.
x=[-10:10];
y=2*x;
stem(x,y);
x = 0:50;
y = 3 * ones(1, length(x));
stem(x, y);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?