String split in matlab - matlab

I have a cell with value WORD = '00000'. But I just want to take the 4th and 5th value.
I already tried KATA=WORD(4:5),
I also tried KATA=WORD{4}
But still cannot.
I got this error message :
Index exceeds matrix dimensions.
How to split it? It's in <cell> type.

First you need to index the content of "first" (and only) cell element with curly brackets {} and then you can index the vector (). Therefore you need:
WORD = {'12345'}
output = WORD{1}(4:5)
which gives:
output =
45

You might have something like this
>> word = {'00000'};
This is a 1x1 cell array containing a 1x5 char array. To index into the char array, you first need to index into the cell array, which you do with
>> word{1}
ans =
00000
And now you can index the 4th and 5th element
>> word{1}(4:5)
ans =
00

Related

Extract a specific part of cell array values

I have a cell array with values similar to the following one
13:41:54.879
I would like to extract only 13:41 part of the given value and mitigate the rest. I tried various combinations of extractBefore() and extractAfter() but couldn't get it.
You can use a regular expression to match the pattern "digits, colon, digits":
c = {'13:41:54.879', '1:22:33.45679'};
result = regexp(c, '\d+:\d+', 'match', 'once');
gives
result =
1×2 cell array
{'13:41'} {'1:22'}

How to convert text (cell) into ascii code? (Matlab)

Is there a way in matlab to convert a text (type is cell) into ascii code? All the ways i googled did not work like double (text.txt). An error occurs "cell cant be converted to double". I can use a converter outside of matlab, but is it possible to convert it in the programm? Text is 380 words.
Thanks a lot!
To access the contents of a cell array, even if it only has one element, you have to index it using curly brackets. Round brackets give you another cell array.
>> mytext = {'foo', 'bar'}
mytext =
1×2 cell array
'foo' 'bar'
>> mytext(1)
ans =
cell
'foo'
>> mytext{1}
ans =
foo
If I understand correctly, you have a cell array (even if it's only 1 x 1) containing a character vector, and you want to convert the character vector into a vector of doubles:
>> double(mytext{1})
ans =
102 111 111
If that isn't the answer to your problem, please edit your question with more detail about what you're trying to do, showing your code and any error messages you get.

Split a Cell Array

I have a 150X1 cell array. Within the array there are multiple data types. The first cell contains 0.9VA = 1.012207; the second: 0.9VA_CLK = 0.020752; and so on like this (for the most part). I would like to split the cell into two cells using the = as the delimiter. Thus, {1,1}: 0.9VA and {1,2}: 1.012207; {2,1}: 0.9VA_CLK and {2,2}: 0.020752; so on and so forth. I have tried converting them to strings and then using strsplit; however, I run into problems because the string arrays are variable in size.
If there is any other information that I can provide please let me know. Thank you for your help and time in advance.
You can indeed apply strsplit to each of the strings (char arrays) in the cell array. To do so, you can use cellfun:
c{1} = '0.9VA = 1.012207';
c{2} = '0.9VA_CLK = 0.020752';
c{3} = 'CSIPhgenSWoffList = [0, 0, 0, 0]';
c{4} = 'SomethingElse = [0.020752, 0.24564]';
c = cellfun(#(x)strsplit(x,'='),c,'UniformOutput',false);
c = cat(1,c{:});
I use a small example cell array c here, containing four strings, I hope this is representative. I apply strplit to each cell in c using cellfun(x,'='), which splits at the equal sign and returns a cell array with cell arrays. That is, each string in c is turned into a cell array with 2 strings (e.g. '0.9VA ' and ' 1.012207'. This does leave some spaces at the beginning and end of the strings.
The next line, cat, converts this cell array of cell arrays into a two-dimensional cell array. The final output is a cell array c containing the same number of rows as the original cell array, and with 2 columns. The first column corresponds to the part before the equal sign, the second column to the part after the equal sign.
To remove the spaces, you can use cellfun again, with strtrim:
c = cellfun(#strtrim,c,'UniformOutput',false);

MatLab find column number of text cell array

I have a cell data type matrix containing a header and a large number of rows.
sample data:
set press dp
32.7045 17.805965 123.75047
32.690094 17.80584 123.74992
32.6232 17.815094 123.790115
I am trying to find the index of a specific column using the strcmp command to search through the all the data.
dpCol = strcmp([data{:}], 'dp')
This always returns
dpCol =
0
Am I using the data cell type wrong or something? Thank you!
Try using cell notation to yield just the 1st row, EG:
data(1,:) = {'set','press','dp'}
instead of unpacking* the entire cell array since strcmp can operate on cell arrays.
>>> data = {'set' 'press' 'dp'
32.7045 17.805965 123.75047
32.690094 17.80584 123.74992
32.6232 17.815094 123.790115}
data =
'set' 'press' 'dp'
[32.7045] [17.8060] [123.7505]
[32.6901] [17.8058] [123.7499]
[32.6232] [17.8151] [123.7901]
>>> col_idx = strcmp(data(1,:),'dp')
col_idx=
0 0 1
Then return the dp using the logical indices and cell2mat...
>>> dp = cell2mat(data(2:end,col_idx))
dp =
123.7505
123.7499
123.7901
or unpack* and concatenate the comma separated list
>>> dp = [data{2:end,col_idx}]
dp =
123.7505 123.7499 123.7901
As an alternative try cell2struct.
>>> datastruct = cell2struct(data(2:end,:),data(1,:),2)
datastruct =
3x1 struct array with fields:
set
press
dp
Then dp is ...
>>> dp = [datastruct.dp]
dp =
123.7505 123.7499 123.7901
* Using the colon operator inside curly braces unpacks an cell array into a comma separated list. Using square brackets horizontally concatenates the comma separated list which returns a character array set pressdp{{{ since the first item in the list is a character array. The garbage characters between and after 'set', 'press' and 'dp' are caused by reading the doubles as char. IE: char(32.7045) is the ASCII equivalent of whitespace. The arrays always get unpacked as column.

Removing rows by indexing

I have cell array of approx 300 rows. The array has 6 columns. In the 6th column very are lots of rows with zeros. I want to remove these rows from my array.
I am using the line below but getting an error message saying "Undefined function 'ne' for input arguments of type 'cell'."
myData = myData(myData(:,6) ~= 0);
If it's a cell array of numerals, try this -
myData(~vertcat(myData{:,6}),6)={[]}
or
myData(~cell2mat(myData(:,6)),6)={[]}
or this, which is purely from #chappjc's comments
myData(~[myData{:,6}],6)={[]}
If it's a cell array of characters, try this -
myData(~str2double(myData(:,6)),6)={''}
Edit 1: If you would like to remove entire rows if the corresponding elements in the 6th column are zeros, index the whole row using :. Thus, the above codes would change to the following forms respectively :
myData(~vertcat(myData{:,6}),:)={[]}
myData(~cell2mat(myData(:,6)),:)={[]}
myData(~[myData{:,6}],:)={[]}
myData(~str2double(myData(:,6)),:)={''}
Edit 2: If you would like to remove the rows from the cell array that have all empty cells, you may use this -
myData(all(cellfun('isempty', myData),2),:) = []