how to add blank columns to table
310 次查看(过去 30 天)
显示 更早的评论
I have a table with 100-200 rows and ~400 columns. I want to add a blank (empty) column at 3-4 specified points in between some of the existing columns. This will segregate the data and make it more readable...sort of like adding a column in Excel.
I can't seem to find the right command(s). Any help?
1 个评论
the cyclist
2017-7-24
Just to clarify, because loose terminology is pretty common on this forum: do you mean a variable of class table?
采纳的回答
the cyclist
2017-7-24
If you do mean a table (per my comment above), then will this work for you?
x = [1 2 3 4]';
y = [2 3 4 5]';
emptyCol = cell(4,1);
T = table(x,emptyCol,y)
3 个评论
the cyclist
2017-7-25
Not that I can think of, but you could fill the "empty" column with a vertical bar instead, and that might look a bit more appealing:
x = [1 2 3 4]';
y = [2 3 4 5]';
e = repmat('|',4,1);
T = table(x,e,y)
更多回答(1 个)
Hobart
2021-2-9
I've looked at most, if not all, of the answers to this and related questions. We need a simple easy way of adding and inserting both empty columns and empty rows onto and into tables in Matlab (Mathworks, are you listening?). NONE of the answers I have found meet this basic need. All the answers in some form or the other require one to know a priori what you want in the table before you create the table. Although this indeed sounds like common sense program design practice, the strength of Matlab is being able to use the function writing experience AS your design process. If I already know all my program's design requirements a priori, then I might as well just write the program in C or some other faster running language, and be done with it. But that is usually not the case, whence Matlab shines... except in this issue of adding and inserting columns (variables if you must) and rows. Defining rows and columns (variables) in tables should be absolutely no different that allocating memory for a variable: in most languages one MUST allocate the variable and its memory before putting data into it. It is this analog we desire and need with Tables. With that as background, now to a solution that may be of use for some.
After futzing with a number of the more clumbsy methods described in a number of the answers for this and other related questions (this question has beend asked quite a lot in different ways because of its freqiency of need), the following is a simple way to add "empty" (or near empty) columns to a table that may be of use.
Either have Table "T" or make an empty Table:
T = table();
Add a first column to it using one of the following two syntaxes (or others as needed):
T.ONE = NaN; %Or T.ONE = "";
The first is for a numbers column type, and the second for a strings type (others may work as well).
Now, lets make it have 2 rows for the demonstration:
T = [T; T]
begetting:
T =
2×1 table
ONE
___
NaN
NaN
Now the magic. To add a second column:
T.TWO(:) = NaN; %Or if you dont want strings, then: T.TWO(:) = ""
Hence:
T =
2×2 table
ONE TWO
___ ___
NaN NaN
NaN NaN
And now a third column of strings:
T.THREE(:) = ""
T =
2×3 table
ONE TWO THREE
___ ___ _____
NaN NaN ""
NaN NaN ""
As mentioned above, other scalars may work as well. This does not work if trying to assign the new columns as an empty value of "[]" since that deletes the column (a syntax which I consider in conflict with the long standing Matlab syntax to define a scaler as an empty via "A = [];"). And it also does not work with " '' " (double single quotes: same comment applies). But " ' ' " (single quote, space, single quote) does work because its actually just a char of length one. But that might work for you as an empty column surrogate if you want text, except the length (number of spaces) will need to be the same length as the data you want to stick in there and each row will have to have the same length of char.
One last trick is to use an empty cell as the defining placeholder:
T.FOUR(:) = {[]}; %Or {''} , or really anything inside of the cell "{}" to include {NaN}, but "{}" by itself fails
T =
2×4 table
ONE TWO THREE FOUR
___ ___ _____ ____
NaN NaN "" []
NaN NaN "" []
Wherein column FOUR elements are cells containing [] (or alternatively other things like '' as described above)
The key thing that makes this appear to work is the "(:)" index assignment of the table's column (Variable) name in the structure method for working with tables. There may be other variants on this theme that also work, but this is what got me what I wanted for that piece of my code. Hope it might help.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!