MATLAB Answers

printing a table made of 3 arrays

1 view (last 30 days)
Ethan Anderson
Ethan Anderson on 25 Jul 2021
Answered: dpb on 25 Jul 2021
Hello,
I'm sorry if this is a very simple quiestion but I am having trouble finding the reference I need.
I have 3 arrays of 100 points (x values and the respective values of two functions).
I am attempting to print them into a table of three columns (x, p1, p2)
I tried disp(table(x, p1, p2) and it only displayed [1x100 array] instead of any data.
Any advice someone could give me?
I just need to save the data of these arrays into a table than I can save elsewhere. Is there a way to just copy the data and put it in excel or something?
thanks!

Accepted Answer

Simon Chan
Simon Chan on 25 Jul 2021
You can write the entire table to excel using the following code:
However, please make sure that they are all column vectors and having same number of rows.
writetable(table(x,p1,p2),'result.xlsx','UseExcel',true,'WriteMode','append');

More Answers (1)

dpb
dpb on 25 Jul 2021
disp() just displays something to the command window without the "variable =" prefix; it doesn't save anything. Also, the hint that it showed a 1x100 array shows that your variables are row, not column vectors. table will put a row vector on a row in a table as an array; you need to store them to a table as column vectors instead...
tData=table(x(:),p1(:),p2(:),'VariableNames',{'X','P1','P2'});
The (:) will ensure the variables are column vectors but table won't pick up the variable names with it in the argument list as MATLAB creates a temporary; hence the variable names given (besides that I think the capitals look nicer with numeric suffixes than do lowercase). Obviously, "salt to suit!"
Use writetable to save to a file

Tags

Products

Community Treasure Hunt

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

Start Hunting!