How to add scientific units to a matrix?

h1 = sat_interpolated(:,2)*kJ/kg;
Is there a way to assign units to a vector or matrix?

1 Comment

You might also be interested in my FEX submission num2sip, which converts a numeric value to a string with the SI Prefix:
>> num2sip(1e6)
ans = '1 M'
This can be very useful for creating documents and figure text automatically.

Sign in to comment.

 Accepted Answer

You just store the name of them in a separate string or cell array or table column, whichever is easiest:
theUnits = 'kJ/kg';
A string is easiest but you can use a table or cell array if the units will change on a number-by-number basis (which is highly doubtful).

More Answers (1)

You will need to create a new class of arithmetic values that has a display() method that formats the values the way you want. You might be able to subclass double() so that you do not need to work out all of the pure arithmetic functions (though it would certainly be more complicated than that if you wanted to be able to multiply or divide items with different units and have it automatically figure out what the appropriate units are.)
Typically it is much easier to just write a small display routine that formats the array the way you want, and then invoke the routine at need.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!