Matlab: Combining columns of cell array to single cell array while keeping the ' ' - matlab

I have a cell array that looks like this
names =
1×8 cell array
Columns 1 through 3
{'CRCGN014_HEPG2_…'} {'CRCGN013_HEPG2_…'} {'PCLB003_HT29_24…'}
Columns 4 through 6
{'PCLB003_HA1E_24…'} {'PCLB003_HA1E_24…'} {'PCLB003_HCC515_…'}
Columns 7 through 8
{'PCLB003_HT29_24…'} {'PCLB003_HCC515_…'}
and I want to combine them for it to look like:
{ 'CRCGN014_HEPG2_…' , 'CRCGN013_HEPG2_…' .... }
I've tried to use:
cat(2, names{:})
strjoin(names,"', '")
names[{:}]
but to no avail. Can someone let me know if it's even possible to format it that way?
I saw many posts on how to combine the strings but I want to keep the strings intact and with a "," between each string in the cell array

Is there a specific reason to why you would like this format in particular? If knowing the purpose, it might also be easier to adapt the solution accordingly.
Currently, your array names is a character array, meaning that it's functionality is different from Matlab's string arrays. More information on the difference and similarities between the two may be found in the documentation on Characters and Strings.
Presuming it does not affect your following code, I believe converting your array to a string array might be the solution you are looking for. See the following example:
char_arr = [{'A'}, {'B'}, {'C'}]
string_arr = string(char_arr)
producing the output:
char_arr =
1×3 cell array
{'A'} {'B'} {'C'}
string_arr =
1×3 string array
"A" "B" "C"
Edit Rereading your question, I believe I might have misunderstood. Are you looking for one string which combines all entries in names, but keeping the ' and a comma between?
If so, then perhaps this solution might work:
char_arr = [{'A'}, {'B'}, {'C'}]
string_arr = string(char_arr)
string_arr = strcat("'", string_arr, "'")
string_arr = join(string_arr, ',')
producing
char_arr =
1×3 cell array
{'A'} {'B'} {'C'}
string_arr =
1×3 string array
"A" "B" "C"
string_arr =
1×3 string array
"'A'" "'B'" "'C'"
string_arr =
"'A','B','C'"

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'}

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);

Converting cell array of string arrays to a double array

I have a 55X1 cell array. Each cell contains a 1X178 string array of numbers. I would like to convert all the cells to a double array, but in such a way that it forms a 55X178 double array.
Take, for example, the 55X1 cell array dataCellOut = {each cell has a 1X178 string}. I can use: na=str2num(dataCellOut{1}) and this will output a 1X178 double array. I have tried using: na=cellfun(#str2num, dataCellOut, 'UniformOutput', false) and this does not work (error: "input must be a character vector or string scalar"). I have worked on this for awhile to no avail.
I hope this makes sense and if there is anything else that I can offer please don't hesitate to let me know. Thank you in advance!
According to the documentation to str2num:
The str2num function does not convert cell arrays or nonscalar string arrays, and is sensitive to spacing around + and - operators. In addition, str2num uses the eval function, which can cause unintended side effects when the input includes a function name. To avoid these issues, use str2double.
str2double, however, does just as you want:
X = str2double(str) converts the text in str to double precision values. [...] str can be a character vector, a cell array of character vectors, or a string array. [...] If str is [...] a string array, then X is a numeric array that is the same size as str.
Thus, this should work:
na = cellfun(#str2double, dataCellOut, 'UniformOutput', false);
na = cat(1,na{:});
This simple statement works for me.
str2num(char(cellstr_array))

Matlab cell array parsing

I have a large cell array and I'm trying to vectorize some string parsing. The cell is 100000 x 1 and looks like this:
data =
'"2016-07-27T14:18:08.519Z"'
'"2016-07-27T14:18:16.549Z"'
'"2016-07-27T14:18:21.544Z"'
'"2016-07-27T14:18:27.517Z"'
I want to parse this into two cell arrays that look like this:
date_str, which would look like this:
'2016-07-27'
'2016-07-27'
'2016-07-27'
'2016-07-27'
time_str, which would look like this:
'14:18:08.519'
'14:18:16.549'
'14:18:21.544'
'14:18:27.517'
I have looked at using cellfun(#strsplit,data), but it doesn't let me specify a delimiter for the "strsplit" function.
You can use regexprep to remove the double quotes, and then regexp (with the 'split' option) to split at the desired character. I'm assuming the splitting criterion is simply the occurrence of 'T'.
data = regexprep(data, '^"|"$',''); % remove double quotes
result = regexp(data, 'T', 'split'); % split at 'T'
result = vertcat(result{:}); % un-nest cell array
date_str = result(:,1);
time_str = result(:,2);

How to display selected entries of an array of structures in MATLAB

Suppose we have an array of structure. The structure has fields: name, price and cost.
Suppose the array A has size n x 1. If I'd like to display the names of the 1st, 3rd and the 4th structure, I can use the command:
A([1,3,4]).name
The problem is that it prints the following thing on screen:
ans =
name_of_item_1
ans =
name_of_item_3
ans =
name_of_item
How can I remove those ans = things? I tried:
disp(A([1,3,4]).name);
only to get an error/warning.
By doing A([1,3,4]).name, you are returning a comma-separated list. This is equivalent to typing in the following in the MATLAB command prompt:
>> A(1).name, A(3).name, A(4).name
That's why you'll see the MATLAB command prompt give you ans = ... three times.
If you want to display all of the strings together, consider using strjoin to join all of the names together and we can separate the names by a comma. To do this, you'll have to place all of these in a cell array. Let's call this cell array names. As such, if we did this:
names = {A([1,3,4]).name};
This is the same as doing:
names = {A(1).name, A(3).name, A(4).name};
This will create a 1 x 3 cell array of names and we can use these names to join them together by separating them with a comma and a space:
names = {A([1,3,4]).name};
out = strjoin(names, ', ');
You can then show what this final string looks like:
disp(out);
You can use:
[A([1,3,4]).name]
which will, however, concatenate all of the names into a single string.
The better way is to make a cell array using:
{ A([1,3,4]).name }