Hi there,
Raghav Gupta said:
% Why not double quotes here as in cout of C++?
Matlab recognizes single quotes for input of strings.
Raghav Gupta said:
% To use what disp or display or fprintf here?
fprintf is to useful when displaying strings for which you want to control the format of the data inside your string.
disp is simply used to display a string, such as the one you typed above. Note, however, that using
disp does not allow you to format your string whatsoever, and should only be used when displaying simple strings like these.
NB: There's no such function
display in Matlab. Use
disp.
An example of using
fprintf is the following:
>> fprintf('My age is %d\n', 20)
>> My age is 20
You can type
help fprintf into the command window to get more information on this. In this case, from what I gather,
disp will serve your purpose just fine.
Raghav Gupta said:
arr[
i][
j]=
input(''); % Here error is coming of unbalanced parentheses or brackets.
Here I am assuming that you want to store the user's input into a matrix element.
Do note that to call an element of a matrix by its row and column, the proper way is by doing it this way:
>> mat(r,c) = input('Please input an element: ');
where mat is the name of your matrix (it is stored as a variable in your workspace) - you have to define it first above this line of code (outside your for loop)
Raghav Gupta said:
display('The matrix elements are:');
for(
i=0;i<m;
i++)
for(
j=0;j<n;
j++)
disp(arr[
i][
j]);
disp('\t');
end
fprintf('\n');
end
Matlab's syntax is different from C++. Use the same format as you did for the for loop above. Use the colon operator to specify the range for i and j.