(Matlab) How to check if cell array contains string - matlab

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.

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

multi structure output to list

Below is some code that will work in extracting out the a cell array of ID names from the structure. It uses a for loop to achieve this. However I am wondering if there is a way to perform the same task without using a forloop?
tft(1).Id = 'Name1';
tft(1).Desc = 'goes by the name';
tft(2).Id = 'Name2';
tft(2).Desc = 'hates the name';
for a=1:length(tft)
list{a} = tft(a).Id
end
There is a documentation page dedicated to this question: Access Elements of a Nonscalar Struct Array.
Since doing tft.Id returns a comma separated list, you can convert it into a cell array directly by enclosing it in curly braces:
list = {tft.Id};

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.