Delete a cell array column - matlab

Placed simple values into the cell array for testing.
model{1,1}=1;model{1,2}=2;model{1,3}=3;
model{2,1}=4;model{2,2}=5;model{2,3}=6;
i=2;//I want to remove the second column
temp={ model{:,1:i-1} model{:,i+1:size(model,2)} }
I wanted a result like this:
temp =
[1] [3]
[4] [6]
But I'm getting this:
temp =
[1] [4] [3] [6]
How can I get this right?
p.s: for anyone working on Cell Arrays, there's a nice technique for appending here.

You can reshape or delete the cells themselves using ()-addressing.
model(:,2) = [];

You have to transpose the two pieces, and change some parentheses:
temp= [{ model{:,1:i-1}}' {model{:,i+1:size(model,2)}}']

there is a function called fun_removecellrowcols, which removes specific row/columns indicated by the user. This affects the dimensions of the cell, due to the row/cols removal.
http://www.mathworks.com/matlabcentral/fileexchange/46196-fun-removecellrowcols
Regards,
José

Related

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

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

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

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),:) = []

reading cell array using jmatio

I am attempting to read from a matlab cell array of strings using the java library jmatio
This is my code
MatFileReader matreader=new MatFileReader ("filepath");
MLarray array= matreader.getMLArray ("cellData").contentToString ());
If I print out array I get an out put that shows me an array with the correct dimensions but in place of the cell elements it tells me the size of the character array in the cell. For example if the first cell contained a string of 5 characters it would show the following
[1×5 char array]
The information is correct but I would like to access the actual information of the cell.
When I used MLCell as in the following I only get the dimensions of the array itself .
Int [] dims = matreader.getMLArray.getDimensions ();
MLCell cellarr=new MLCell("celldata", dims);
Does anyone know the correct usage.
Thank you in advance.
You have to use the get-Function to get an element from the MLCell.

search through cell elements that contain a string, then delete it

Hi I want to search a cell array for any elements containing the letter 'x'. I can delete a cell element by doing the following:
mycell(3) = []
but trying to search through the elements id the difficult part. I am using:
offending_cell = strcmp('x', mycell);
however this is just picking out all elements regardless of x appearing in them. Anyone have any suggestions?
There you go:
ind = (~cellfun('isempty',(regexp(mycell,'x'))));
This gives logical indices for cells that contain 'x'. If you want to delete those cells:
mycell(ind) = [];
The problem with you apporach was that strcmp looks for exact matching, not if the string contains a given character.