MATLAB Answers

0

How to add header to a table?

Asked by Bai chen on 17 Feb 2019
Latest activity Commented on by Walter Roberson
on 17 Feb 2019
Hello,
How can I add header to my table below ?
I want to add header
header={'Call price','Standard error','Number of paths'}
to the table.
S0=30;
K=32;
r=0.03;
sigma=0.2;
T=1;
n=365;
%m=500; % number of simulations
j=1;
for i=500:500:10000
[call_price,standard_error,numberOfpaths]=Call_Simulation(S0,K,r,sigma,n,i,T);
Call_Price(j)=call_price;
Standard_Error(j)=standard_error;
NumberOfPaths(j)=numberOfpaths;
j=j+1;
end
header={'Call price','Standard error','Number of paths'};
T=table(Call_Price',Standard_Error',NumberOfPaths');
disp(T);
error_table.PNG

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Star Strider
on 17 Feb 2019
 Accepted Answer

Use the 'VariableNames' (link) name-value pair with your ‘header’ cell array.

  3 Comments

I looked through the link.
It does not help.
The elements of ‘header’ must be valid MATLAB variable names. Then it works.
Try this:
T = table(rand(5,1),rand(5,1),rand(5,1))
header={'Call_price','Standard_error','Number_of_paths'};
T.Properties.VariableNames = header
producing:
T =
5×3 table
Var1 Var2 Var3
_______ _______ ________
0.48861 0.54681 0.67914
0.57853 0.52114 0.39552
0.23728 0.23159 0.36744
0.45885 0.4889 0.98798
0.96309 0.62406 0.037739
T =
5×3 table
Call_price Standard_error Number_of_paths
__________ ______________ _______________
0.48861 0.54681 0.67914
0.57853 0.52114 0.39552
0.23728 0.23159 0.36744
0.45885 0.4889 0.98798
0.96309 0.62406 0.037739
That should do what you want.
Note that table() objects do not support arbitrary column headers. They can store VariableDescription for each column, but summary() is about the only thing that displays the description.

Sign in to comment.