Problem with Right Justified Strings

4 views (last 30 days)
Tommaso Belluzzo
Tommaso Belluzzo on 27 Jul 2017
Answered: Walter Roberson on 27 Jul 2017
I'm using Matlab R2014b (that's why I cannot use strings, but only char vectors). Working inside a class, I have to take data from a table variable, format it following my needs, and then insert it into a GUI table (an instance of uitable, to be exact):
function UpdateTable(this)
siz = size(mydata);
tab = cell(siz);
tab(:,1) = num2cell(this.Data.ID);
tab(:,2) = cellstr(datestr(this.Data.Date,'dd/mm/yyyy'));
tab(:,3) = arrayfun(@(x){MyClass.TypeDef1{x,1}},this.Data.Type1);
tab(:,4) = arrayfun(@(x){MyClass.TypeDef2{x,1}},this.Data.Type2);
tab(:,5) = arrayfun(@(x){MyClass.FormatNumber(x)},this.Data.Value);
this.UITable.Data = tab;
properties (Access = private, Constant)
TypeDef1 = {
'A1' 'Name A1';
'B1' 'Name B1';
'C1' 'Name C1';
'D1' 'Name D1';
TypeDef2 = {
'A2' 'Name A2';
'B2' 'Name B2';
'C2' 'Name C2';
'D2' 'Name D2';
methods (Access = private, Static)
function str = FormatNumber(num)
persistent df;
if (isempty(df))
dfs = java.text.DecimalFormatSymbols();
df = java.text.DecimalFormat();
str = char(df.format(num));
Everything is working fine. Now I would like to right justify the strings to be inserted in columns 1 and 5, to improve the table readability. I found the Matlab function that suits my needs, strjust. Reading the documentation, I saw that it can be used with cell arrays of char vectors, so I modified part of my UpdateTable code as follows:
tab(:,1) = strjust(num2cell(this.Data.ID));
tab(:,5) = strjust(arrayfun(@(x){MyClass.FormatNumber(x)},this.Data.Value));
The first line produces the error conversion to cell from char is not possible, while the second one produces no changes (strings are still not justified).
Jan on 27 Jul 2017
@Tommaso Belluzzo: Please post the relevant data. This is more useful than posting code, which is not concerned by the problem.
tab(:,1) = strjust(num2cell(this.Data.ID));
tab(:,5) = strjust(arrayfun(@(x){MyClass.FormatNumber(x)},this.Data.Value));
Hwt is the contents of "this.Data.ID" and "this.Data.Value"? Is
working as expected and what does it reply? If the problem concerns strjust only, simply post the output of arrayfun, such that the readers can concentrate on the actual problem.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Jul 2017
Use cellstr to convert a char array to a cell array of character vectors.
That said, you would need to use a fixed width font to have any hope that uitable would not mess up the spacing . Even then it will tend to mess it up, potentially forcing you to resort to coding the uitable content as HTML that encodes each entry as a tabledata inside its own individual HTML table... Something like that, the details do not come to mind at the moment, partly because the details did not make any real sense and had to be arrived at by pure experimentation.


Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!