Removing rows by indexing - matlab

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

Related

(Matlab) How to check if cell array contains string

I am trying to grab data from an excel spread sheet and grab only the information from cells that match a string. Eg. if cell A10 contains the word 'Canada' it should return that cell.
I have tried using strcmp(https://www.mathworks.com/help/matlab/ref/strcmp.html) to check if string in argument 1 is contained in a cell array containing many strings, the second argument
[num,txt,raw] = xlsread('\\Client\C$\Users\Fish\Desktop\dataset\dataset.csv');
mytable = cell(raw);
for i = 1:54841
array_index = i;
string_index = mytable(i,2);
string_eastern = {'Canada', 'Ontario'};
if strcmp(string_index,string_eastern);
fprintf('%d\n',array_index)
end
end
In the above example if my string_eastern only contains one element, say 'Canada', it will return the index value of every instance of 'Canada'. If I add more elements I expect it would return index values for every instance where string_index would match with a string contained in string_eastern. However I get no results at all if I add more elements.
Pretty much I wanted to check my string_index agaisnt string_eastern, if the values match then I want it to return that cell value. This works when string_eastern is only 1 element but does not work with more than 1.
To access cell contents, use the curly brackets {}. So if I wanted to access the first element of my cell, I would say this:
string = cell{1};
Read more on the MATLAB Documentation about cells to learn more and to answer any of your further questions.

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

convert number from cell to double, cell2mat not working

I think I'm losing my mind.
I have a 3 x 2 cell which looks exactly like below.
Region Code
US 1
EU 2
I then have the following code to determine the row number for the EU region.
eq_code_index = find(ismember(fund.type_des(:, 1), 'EU'));
eq_code = cell2mat(fund.type_des(eq_code_index, 2));
eq_code_index returns 3 which is correct (row headers are included in the output). So I want the value in row 3, column 2 which is 2. I then use cell2mat to convert it from a cell value to an integer however it doesn't work the value is of type char? Haven't a clue why cell2mat isn't working?
Update
Even if I do the following two lines of code below I can't get the codes into a vector, they turn into char's
codes = fund.type_des(2:end, end);
codes = cell2mat(codes)
To access a single element in a cell array, use curly braces:
fund.type_des{eq_code_index, 2};
This is generally simpler than using cell2mat(). If the contents of the cell are chars and you want an integer, you have to perform the conversion. str2num() is one of many options for this:
eq_code = str2num(fund.type_des{eq_code_index, 2});

String split in 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

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.