Adding two strings same row size

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 个评论

Bastiaan Pierik's incorrectly posted and accepted "answer" moved here:
Many thanks for all the replies! So there are two methods to do this.

请先登录,再进行评论。

回答(2 个)

To use the join function, your data must first be present as table objects.
Fortunately, that is not necessary. Just use array2table:
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 个评论

Thanks for pointing that out!
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
.
Ok, that’s nice to know, I thought I had to make a another array and join them together like with outerjoin, I am getting errors I doing this. The first two inputs must be tables
Thank you!
It is only possible to join table objects, at least using that function.
(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)
totalstr = 20x8 table
TC1 TC2 TC3 TC4 C1 C2 C3 C4 ___ ___ ___ ___ ___ ___ ___ ___ 46 55 -9 62 55 -2 72 15 16 79 20 48 16 22 1 -10 46 6 59 63 47 -1 31 15 52 50 42 -5 -8 43 79 67 -9 2 23 78 -10 62 -10 73 62 1 39 76 -4 -2 37 21 0 74 16 -3 1 76 50 18 50 33 6 10 56 64 20 58 40 22 26 -8 11 68 43 29 5 13 31 46 -6 19 5 50 0 8 47 5 53 18 0 45 0 4 0 2 59 57 18 -10 54 20 73 44 -6 31 30 55 15 28 9 50 68 -10 -8 53 5 68 33 79 46 65 77 71 20 -4 65 27 -7 23 48 76

7 个评论

Thanks for pointing that out works like a charm.
If I have a file like a txt file , which the data is included like in the table how can I separate the table
if true
TC1 TC2 TC3 TC4 C1 C2 C3 C4
___ ___ ___ ___ __ ___ ___ __
44 47 59 19 25 32 -1 20
8 12 2 -2 -5 17 63 79
49 66 -5 63 80 29 50 33
53 66 51 26 50 17 31 57
31 31 33 49 10 15 38 50
4 25 35 41 -2 2 37 62
end
Separating one column against the time?
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
ans = 20×1
29 -5 41 17 3 8 78 23 57 48
Thanks for both methods!
The latter is the more abstract one.
If this was written in a txt file with the same size and columns would Matlab still be able to filter it the same way? Or do I require other methods?
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.
Hi Cris,
What I am trying to do is to get a substitute for understanding reading out my arduino directly and the data from a txt file. To do so I have to create a substitute table that resembles the data I am collecting from my microprocessor. I am working from my cellphone because I am not connected to the Matlabserver at the moment. Second part is iPhones don’t have access to files that are readout on the directory, third the buffer in Matlab isn’t to big handling large quantities of data.
What I would like to do is read out the uC
TC1;TC2;TC3;TC4;C1;C2;C3;C4' Separated by ;
Below the each temperature and current sensor I have the variables from my arduino
Like:
46 55 -9 62 55 -2 72 15 16 79 20 48 16 22 1 -10 46 6 59 63 47 -1 31 15 52 50 42 -5 -8 43 79 67 -9 2 23 78 -10 62 -10 73 62 1 39 76 -4 -2 37 21 0 74 16 -3 1 76 50 18 50 33 6 10 56 64 20 58 40 22 26 -8 11 68 43 29 5 13 31 46 -6 12 36 48
Do I have to convert the variables first to a str2double? and separate the temperatures on ; or white space?
In generell is a txt file a string or how does Matlab know it is a double?
Thanks for your time and reading this:)
I would like to do some data recording and compare measurements with theory
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by