Adding two strings same row size
Ältere Kommentare anzeigen
First string presents the name of the sensors to be measured: And are separated by semicolons.
str='TC1;TC2;TC3;TC4;C1;C2;C3;C4'
Second string are 8 rows separated by str2= randi([-10,80],20,8)
I had some problems with rand and tells me the rand can’t produce a scalar,
Second problem is adding both both columns with the same length on top of each other
I would like to use join,I get the following error I don’t understand. I am running this on Matlab mobile.
totalstr=join(output_str,str2,'delimiter') Error using join (line 73) Dimension argument must be a positive integer scalar within indexing range.
1 Kommentar
Stephen23
am 27 Jan. 2021
Bastiaan Pierik's incorrectly posted and accepted "answer" moved here:
Many thanks for all the replies! So there are two methods to do this.
Antworten (2)
Star Strider
am 24 Jan. 2021
str={'TC1','TC2','TC3','TC4','1','C2','C3','C4'};
str2= randi([-10,80],20,8);
T = array2table(str2, 'VariableNames',str);
That should do what you want.
4 Kommentare
Bastiaan Pierik
am 24 Jan. 2021
Star Strider
am 24 Jan. 2021
Bearbeitet: Star Strider
am 25 Jan. 2021
My pleasure!
EDIT — (25 Jan 2021 at 12:25)
If you have a time (datetime) vector and you want to include it as the first column of the table, it is easy to concatenate it:
str={'TC1','TC2','TC3','TC4','1','C2','C3','C4'};
str2= randi([-10,80],20,8);
T = array2table(str2, 'VariableNames',str);
DT = table(datetime('now') + days(1:size(T,1)).', 'VariableNames',{'Date_Time'});
T = [DT T]
producing:
T =
20×9 table
Date_Time TC1 TC2 TC3 TC4 1 C2 C3 C4
____________________ ___ ___ ___ ___ __ __ __ __
26-Jan-2021 05:22:57 52 61 0 51 -2 79 44 59
27-Jan-2021 05:22:57 2 23 61 29 2 5 20 57
28-Jan-2021 05:22:57 55 8 16 31 5 13 17 57
29-Jan-2021 05:22:57 0 -3 44 45 7 26 31 -1
30-Jan-2021 05:22:57 0 60 77 -5 18 -4 28 52
31-Jan-2021 05:22:57 48 8 29 18 18 52 22 32
01-Feb-2021 05:22:57 19 25 53 60 9 26 40 9
02-Feb-2021 05:22:57 49 40 58 53 12 79 57 -2
03-Feb-2021 05:22:57 58 10 29 1 71 26 28 64
04-Feb-2021 05:22:57 43 48 49 1 53 46 29 5
05-Feb-2021 05:22:57 57 34 -1 -2 40 4 1 4
06-Feb-2021 05:22:57 11 3 74 -10 6 24 -8 50
07-Feb-2021 05:22:57 56 61 7 28 9 4 16 71
08-Feb-2021 05:22:57 78 -1 14 49 -3 58 18 37
09-Feb-2021 05:22:57 68 16 62 55 73 69 49 53
10-Feb-2021 05:22:57 -3 11 34 38 54 21 77 3
11-Feb-2021 05:22:57 23 38 59 -1 40 52 75 76
12-Feb-2021 05:22:57 23 -2 26 47 18 16 31 39
13-Feb-2021 05:22:57 52 26 14 1 5 38 11 51
14-Feb-2021 05:22:57 44 -1 -7 2 46 65 59 -7
.
Bastiaan Pierik
am 25 Jan. 2021
Star Strider
am 25 Jan. 2021
Thank you!
(Note — There are several join functions, such as the one I refer to in this sentence that applies to string arrays. The functions themselves determine what version of the function to use, depending on the arguments provided.)
What are you trying to create?
The issue is you are mixing data types in an array. I think the way would do this is to use a table.
output_str={'TC1' 'TC2' 'TC3' 'TC4' 'C1' 'C2' 'C3' 'C4'};
str2= randi([-10,80],20,8);
totalstr = array2table(str2,'VariableNames',output_str)
7 Kommentare
Bastiaan Pierik
am 24 Jan. 2021
Bastiaan Pierik
am 25 Jan. 2021
Bearbeitet: Bastiaan Pierik
am 25 Jan. 2021
Sorry, I don't understand what you mean by separate the table. What are you wanting your ifstatement to do?
Each column is considered a variable. This page shows you how to access data in a table. The simplest way is to use dot notation: tblName.varName
output_str={'TC1' 'TC2' 'TC3' 'TC4' 'C1' 'C2' 'C3' 'C4'};
str2= randi([-10,80],20,8);
totalstr = array2table(str2,'VariableNames',output_str);
% Get values for TC3
totalstr.TC3
Bastiaan Pierik
am 25 Jan. 2021
Cris LaPierre
am 25 Jan. 2021
I'm not sure I undersand what you want to do. You can load data from text files, if that is what you are asking.
Bastiaan Pierik
am 25 Jan. 2021
Cris LaPierre
am 25 Jan. 2021
Share an example of the text file you are trying to read. Note that I am using my laptop with the full version of MATLAB, not MATLAB Mobile.
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!