How do I add a column of strings to columns of data to form a table?
124 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Travis Briles
am 16 Jul. 2021
Beantwortet: Vineet Joshi
am 19 Jul. 2021
NOTE: THIS ISN'T AS LONG AS IT LOOKS LIKE!
..........
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.
Question
How do I fix this? Is there a different datastructure that I should be using for a mixture of strings and numbers?
Thanks!
3 Kommentare
Walter Roberson
am 16 Jul. 2021
data_table_Full_B = table(DATEinfo_COLUMN_B, var1, var2, var3);
Akzeptierte Antwort
Vineet Joshi
am 19 Jul. 2021
Hi
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]
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}
Finally your problem can be addreassed in two ways.
- Create a cell array and convert it to a table.
var1 = 10;
var2 = "MATLAB";
var3 = {var1 var2};
Sol_1 = cell2table(var3)
2. Directly create a table from the variables.
var1 = 10;
var2 = "MATLAB";
Sol_2 = table(var2,var1)
Hope this was helpful.
Thanks
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos 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!