Home computickets Outputting a string in MATLAB using the fprintf function

# Outputting a string in MATLAB using the fprintf function

Author

Date

Category

Hello, tell me, please. I created a matrix

``````b = 250: -50: 50
a = linspace (-27.53.5)
w = [a; b]
``````

and now I need to output it using `fprintf `with the general title `MatrixW: `. I’m trying to do it this way

``````fprintf ('MatrixW:% d;% d;% d;% d;% d \ r \ n', w )
``````

There is a problem here more important than the heading: fprintf gathers matrix elements column by column , not row by row. As a consequence, the matrix

``````- 27 -7 13 33 53
250 200 150 100 50
``````

is output as

``````MatrixW: -27; 250; -7; 200; 13
MatrixW: 150; 33; 100; 53; 50
``````

which is hardly acceptable. Replace `w `with `w '`.

By design, the fprintf command generates and prints a string over and over again as long as there is data to output. Therefore, if the title should not be repeated, it should be displayed separately. For example:

``````disp ('MatrixW:'); fprintf ('% d;% d;% d;% d;% d \ n', w ');
``````

``````disp ('MatrixW:');
fprintf ('% d;% d;% d;% d;% d \ n', w ');
``````

this is already a crutch. not only are the columns and rows confused, but also to write more.
when given:

``````A = [1 2; 3 4];
B = [5 6; 7 8];
C = A + B;
``````

it’s easier:

``````disp ('Sum of A and B:');
disp (B);
``````

there is another option as `disp (sprintf ('matrix% g% g \ n', A)); `
but it also messed up with rows and columns: /

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.