How to combine header row and column label for logarithmic tables?

2 次查看(过去 30 天)
I am attempting to build two logarithmic tables, one from values 0 to 5.4 and another from 5.5 to 10. However I cannot seem to merge the column/row header properly (named No.) in the section %% Add a header row and 1st column label. Here is my code so far
%%clear command, variables
clear,clc,close all;
format short g;
%%define table 1 from 0 to 5.4 and table 2 from 5.5 to 10
x1 = linspace(0,0.09,10);
b = 1.0:0.1:5.4;
x2 = linspace(0,0.09,10);
y = 5.5:0.1:10.0;
% new_x has the values of x in multiple rows with the same column values.
% new_y has the values of y in multiple columns with the same row values.
%%Form meshgrid() for both tables
[new_a,new_b] = meshgrid(x1,b)
[new_x,new_y] = meshgrid(x2,y)
%%Create log tables
ab_log_table = log10([new_a+new_b])
xy_log_table = log10([new_x+new_y])
%%Add a header row and 1st column label
ab_header_row = {'No.' x1}
xy_header_row = {'No.' x2}
ab_first_column = {b' 'No.'}
xy_first_column = {y' 'No.'}
%%Output complete tables
logtable1 = table(ab_header_row,ab_log_table)

回答(1 个)

David Ding
David Ding 2017-4-28
Hi Emily,
In the "table" function, unless you are specifying a "Name-Value" pair, all arguments are treated as separate data occupying individual columns of the table. Thus, the line
logtable1 = table(ab_header_row, ab_log_table)
produces an error because the number of rows in the cell array "ab_header_row" has only one row (it is a 1 x 2 cell array) while the second dataset, "ab_log_table", is a 45 x 10 matrix of doubles having 45 rows. Since one dataset has one row while the other has 45 rows, you get the following error:
Error using tabular/countVarInputs (line 227)
All variables must have the same number of rows.
My understanding is that you would like to have a custom column header for the table for the dataset "ab_log_table". This is possible via "Table Properties". For example:
logtable1 = table(ab_log_table)
logtable.Properties.VariableNames = {'X1'};
Note that you cannot have symbols such as "." or "," in your column header. Essentially, each column header represents a variable holding a set of data, and thus the name must conform with MATLAB variable naming conventions.
More examples on naming column headers can be found in this thread:

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by