How do I display a character string array next to a data array?

1 view (last 30 days)
Hello, I am a Matlab noob and I need some help. I wrote this function below to add a random offest to a row vector from -L:L. I had to evaluate this value in several different functions and present the data from each function next to the name of the function. So I made a string containing the names of the functions, but I don't know how present the data next to the function name. Sorry if this doesn't make much sense, but here is my code:
function F = hw5(L) intval = [-L:L] for k = 1:(2*L+1) offset = rand-0.5; val(k) = intval(k) + offset; end offset = val - intval val
x1 = floor(val); x2 = ceil(val); x3 = fix(val); x4 = round(val); x5 = int32(val); x6 = uint32(val); x7 = single(val); x8 = sin(val);
b = ['Floor ',; 'Ceil ',; 'Fix ',; 'Round ',; 'Int32 ',; 'Uint32 ',; 'Single ',; 'Sin ',];
a = [x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 ];
end

Accepted Answer

dpb
dpb on 18 Feb 2014
Edited: dpb on 19 Feb 2014
doc num2str
doc sprintf
etc., ...
One way...
>> b = {'Floor '; 'Ceil '}; % use a cell array for convenience in defining
>> x=rand(2,1); % some numeric values to associate with...
Now make a cell array suitable for framing...
>> cellstr([char(b) num2str(x,3)])
ans =
'Floor 0.815'
'Ceil 0.906'
>>
  2 Comments
Isaiah
Isaiah on 19 Feb 2014
Excellent, thanks a ton! num2str helped me concatenate both matrices perfectly. We haven't really gone through cell arrays much, but I will be sure to figure them out.
dpb
dpb on 19 Feb 2014
NB that one can also write stuff w/ sprintf and friends using formatting expressions altho num2str is quite handy indeed as an aid...

Sign in to comment.

More Answers (0)

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!