N by N dimensional matrix row and column spacing in mat lab script

1 view (last 30 days)
Dears,
It seems that elementary question but It tooks me a while to creat a new multi dimensional matrix in matlab script which is neatly spaced.Can anyone help me in fixing it?
MultD_matrix=[...
1 0 0 0 3 1 0 12.66 1.1 0.9
2 0 0.6 0 3 1 0 12.66 1.1 0.9
3 0 0 0 3 1 0 12.66 1.1 0.9
4 0 0.9 0 3 1 0 12.66 1.1 0.9
5 0 0 0 3 1 0 12.66 1.1 0.9
6 2.6 2.2 0 3 1 0 12.66 1.1 0.9
7 40.4 30 0 3 1 0 12.66 1.1 0.9
8 75 54 0 3 1 0 12.66 1.1 0.9
9 30 22 0 3 1 0 12.66 1.1 0.9
10 28 19 0 3 1 0 12.66 1.1 0.9
11 145 104 0 3 1 0 12.66 1.1 0.9
12 145 104 0 3 1 0 12.66 1.1 0.9
13 8 5.5 0 3 1 0 12.66 1.1 0.9
14 8 5.5 0 3 1 0 12.66 1.1 0.9
15 0 0 0 3 1 0 12.66 1.1 0.9];
FYI while I copy and paste my sscript code here,it automtacially creat neat spacing ,you can check the attched file

Answers (1)

Walter Roberson
Walter Roberson on 16 Apr 2020
>> disp(num2str(MultD_matrix, '%g '))
1 0 0 0 3 1 0 12.66 1.1 0.9
2 0 0.6 0 3 1 0 12.66 1.1 0.9
3 0 0 0 3 1 0 12.66 1.1 0.9
4 0 0.9 0 3 1 0 12.66 1.1 0.9
5 0 0 0 3 1 0 12.66 1.1 0.9
6 2.6 2.2 0 3 1 0 12.66 1.1 0.9
7 40.4 30 0 3 1 0 12.66 1.1 0.9
8 75 54 0 3 1 0 12.66 1.1 0.9
9 30 22 0 3 1 0 12.66 1.1 0.9
10 28 19 0 3 1 0 12.66 1.1 0.9
11 145 104 0 3 1 0 12.66 1.1 0.9
12 145 104 0 3 1 0 12.66 1.1 0.9
13 8 5.5 0 3 1 0 12.66 1.1 0.9
14 8 5.5 0 3 1 0 12.66 1.1 0.9
15 0 0 0 3 1 0 12.66 1.1 0.9
And then copy and paste that into your code.
You can also use, for example,
disp(num2str(MultD_matrix, '%7g'))
  5 Comments
Walter Roberson
Walter Roberson on 16 Apr 2020
No. You can copy the output of the num2str() with your mouse and paste it over your existing matrix.
Otherwise what you are asking me to do is write some code that will read in your .m file, find the matrix, reformat it, and write out the matrix again. That can be done, but I am not going to spend 20+ minutes writing it and testing it just to save you a few keystrokes. My code would have to worry about possibilities such as there appearing to be an assignment to the matrix but which was really commented out with % comments, or which was really commented out with %{ block comments, or which was part of a character vector, or about strange possibilities such as you having coded
MultD...
_...
matrix...
=...
[...
numbers
]
Zeab
Zeab on 16 Apr 2020
Edited: Zeab on 16 Apr 2020
"You can copy the output of the num2str() with your mouse and paste it over your existing matrix"
I just got your point,fixed!
Thanks a lot!

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!