Filter löschen
Filter löschen

How do I add a column of strings to columns of data to form a table?

106 Ansichten (letzte 30 Tage)
I have a table of data consisting of columns of different variables ("data1", "data2" and "data3"). I would like to add a column that describes the datasets in words.
Setup with example data:
My code is similar to the following:
% Generate some fake data
var1 = [21.5000 21.5000 21.5000 21.5000 21.5000].' % 5x1 column
var2 = [ 1500 1500 1500 1500 1500].'; % 5x1 column
var3 = [ -8 -7 -6 -4 1].'; %5x1 column
I can combine this data into a table with variable names in the headers for each column as follows:
data_table = [ [var1] [var2] [var3] ];
data_table_FULL = array2table(data_table,'VariableNames',{'var1', 'var2','var3'}) %returns 5x3 table
The Problem:
Eventually I'm going to merge many tables like this together, where each table is it's own dataset taken on a different day. I'd like to add a text description in each row that describes the date the data was taken on. I've tried two different ways:
method 1
DATEinfo_A = '2021 03Mar 12' %fake description I want for each cell in column.
%attempt at forming a 5x1 column of strings.
DATEinfo_COLUMN_A = repmat(date_info,length(data1),1) % this actually makes a 5x12 char array!
%now build the full table
data_table_Full_A = [ [DATEinfo_COLUMN_A] ]
This returns a 5x15 char array where the variable columns have been converted to wingdings (or something).
method 2
This seemed like I needed to convert the character array to a string array so I changed the apostrophe marks to quotation marks.
DATEinfo_B = "2021 03Mar 12" %use quotation marks to make string array instead of char array
%attempt at forming a 5x1 column of strings.
DATEinfo_COLUMN_B = repmat(DATEinfo_B,length(data1),1) % this is a 5x1 string array. should be what I want...
%now build the full table
data_table_Full_B = [ [DATEinfo_COLUMN_B] [var1] [var2] [var3] ] %->5x4 string array. converts variable columns to strings!
This seemed to get the string array correct but unfortunately forced the other entries in the table to be strings as well.
How do I fix this? Is there a different datastructure that I should be using for a mixture of strings and numbers?
  3 Kommentare
Travis Briles
Travis Briles am 16 Jul. 2021
This seems to make a 5x1 string array where the variables have been merged into the "DATEinfo_B" string.
I was still hoping to get a 5x4 table where first column is just strings and the next 3 columns are numbers. Is that possible?
Walter Roberson
Walter Roberson am 16 Jul. 2021
data_table_Full_B = table(DATEinfo_COLUMN_B, var1, var2, var3);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Vineet Joshi
Vineet Joshi am 19 Jul. 2021
An array in MATLAB usually works with homogeneous data and hence when you try to concatinate a string array and integer array, it typecast the interger to string. You can refer the following code to see this.
var1 = 10;
var2 = "MATLAB";
var3 = [var1 var2]
var3 = 1×2 string array
"10" "MATLAB"
If you want to combine non-homogeneous data types, then you can use a cell array instead, as shown below.
var1 = 10;
var2 = "MATLAB";
var3 = {var1 var2}
var3 = 1×2 cell array
{[10]} {["MATLAB"]}
Finally your problem can be addreassed in two ways.
  1. Create a cell array and convert it to a table.
var1 = 10;
var2 = "MATLAB";
var3 = {var1 var2};
Sol_1 = cell2table(var3)
Sol_1 = 1×2 table
var31 var32 _____ ________ 10 "MATLAB"
2. Directly create a table from the variables.
var1 = 10;
var2 = "MATLAB";
Sol_2 = table(var2,var1)
Sol_2 = 1×2 table
var2 var1 ________ ____ "MATLAB" 10
Hope this was helpful.

Weitere Antworten (0)


Mehr zu Data Type Identification finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by