Problem with Right Justified Strings

3 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;
end
Where:
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';
...
}
end
methods (Access = private, Static)
function str = FormatNumber(num)
persistent df;
if (isempty(df))
dfs = java.text.DecimalFormatSymbols();
dfs.setDecimalSeparator(',');
dfs.setGroupingSeparator('.');
df = java.text.DecimalFormat();
df.setDecimalFormatSymbols(dfs);
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
end
str = char(df.format(num));
end
end
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).
  2 Comments
Geoff Hayes
Geoff Hayes on 27 Jul 2017
Tommaso - from the documentation for strjust, the input can be a cell array of strings though when I call (using R2014a)
strjust(num2cell(42))
this fails with the error The first argument does not contain a cell array of strings...which is different from your error. If this.Data.ID is a scalar, then why not convert it to a string directly as
strjust(num2str(this.Data.ID));
Or is this.Data.ID something other than a (numeric) scalar? Please clarify.
As for the second statement not producing any change, what does
arrayfun(@(x){MyClass.FormatNumber(x)},this.Data.Value)
produce? Are there any trailing whitespace characters that would then be removed and placed at the beginning of the character array?
For example,
>> strjust('string ')
ans = ' string'
>> strjust('string')
ans = 'string'
Since there is no trailing whitespace in the second example, strjust doesn't change the string. If this is the case (with your strings) then you probably want to prepend some leading whitespace characters to your string
>> [' ' 'string']
ans = ' string'
Jan
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
arrayfun(@(x){MyClass.FormatNumber(x)},this.Data.Value)
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.

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!