0

Sorry for my dumb question, i'm new on matlab. I have an matrix array like this

num = [
    4.2, 3, 5;
    3, 12.1, 3.4;
    2, 5.22, 4
]

I just want to display it using center align format, like the example below

enter image description here

but number in num array is dynamic, sometimes on each row contain up to 4 or more number like this

num = [
    4.2, 3, 5, 7.899;
    3, 12.1, 3.4, 89;
    2, 5.22, 4, 9.1
]

I was trying using num2str() function, but it doesn't fit on my case because my data is dynamic (sometimes it always have 2 or 3 more digit of decimal number) here is my code:

num2str('%10.1f \t %10.1f \t %10.1f \n', num);

Is there any other function beside using num2str(), because my array data is dynamic

Jhonny Jr.
  • 363
  • 3
  • 4
  • 15
  • 1
    What are you displaying it in? Just printing it out? If you use e.g. a `uitable` control, I bet you can center the text using the display settings of the control... – Dev-iL Feb 22 '18 at 14:32
  • Can you describe exactly what's wrong with the output of `num2str`? Mine looks about right with `sprintf('%10.3g \t %10.3g \t %10.3g \n', num(:,1:3)')`, except the centre align. The question [Centering strings with printf()](https://stackoverflow.com/q/2461667/5358968) might help too – Steve Feb 22 '18 at 14:36
  • @Dev-iL I just display it on listbox panel – Jhonny Jr. Feb 22 '18 at 14:40
  • @Steve actually there is no problem with the output, i just want to make it center align – Jhonny Jr. Feb 22 '18 at 14:42
  • Can you post what you'd like the exact output to look like for the above two cases? (Replace the image with a code block for each) – Steve Feb 22 '18 at 14:49
  • I know you already have an answer, but [this Q&A](https://stackoverflow.com/a/33484032/3372061) might be related. – Dev-iL Feb 22 '18 at 15:56
  • Related: [How to center-justify a string in text file using fprintf in MATLAB?](https://stackoverflow.com/q/43034610/5358968) – Steve Mar 06 '18 at 11:23

1 Answers1

4

You can centre a string with strjust. Here, I build the individual elements in a loop with sprintf and add a newline character:

num = [
4.2, 3, 5, 7.899;
3, 12.1, 3.4, 89;
2, 5.22, 4, 9.1
];

% Loop over rows (ii) and columns (jj) of num
output = '';
for ii = 1:size(num,1)
  for jj = 1:size(num,2)
    output = [output, strjust(sprintf('%10.4g',num(ii,jj)),'center')];
  end % for jj
  output = [output, '\n'];
end % for ii
fprintf(output)

Output:

   4.2        3         5       7.899   
    3        12.1      3.4        89    
    2        5.22       4        9.1    

You can put this into e.g. an image by using a final call to sprintf:

text(0.5, 0.5, sprintf(output))

Note that this uses a non-fixed width font, so a long line might not look centre-justified. This can be seen by using

num = [999, 999, 999, 999; 1, 1, 1, 1];

MATLAB version R2014a.

Steve
  • 1,579
  • 10
  • 23
  • This is work, but have one problem with mine, so i just want to display it on set(handles.gui) is there any other way how to display on it, because when i put set() inside for loop, it just display the last row – Jhonny Jr. Feb 22 '18 at 15:24
  • @JhonnyJr. I've changed it, should be more flexible – Steve Feb 22 '18 at 15:29
  • Thanks a lot, it works, but i still confused, because '\n' operator is on the output too, it doesn't want to generate a new line – Jhonny Jr. Feb 22 '18 at 15:35
  • @JhonnyJr. try using your `set` method on `sprintf(output)` instead? – Steve Feb 22 '18 at 15:36
  • @JhonnyJr. could be worth searching for other answers along the lines of *using set handles gui with newline*, or posting a new question trying to add `1\n2` to a gui. – Steve Feb 22 '18 at 15:39
  • @JhonnyJr. new example using `text` to put it into an image (I've not used any GUI features in MATLAB) – Steve Feb 22 '18 at 15:44
  • sprintf is work steve, thanks a lot, but i think when using "text" gui style will make it not look so good – Jhonny Jr. Feb 22 '18 at 15:49
  • @JhonnyJr.perhaps my last example is motivation enough for a new question – Steve Feb 22 '18 at 15:53