How do I find unique cells in excel using matlab? i was trying to get all unique strings in some excel field,
so baisicly im trying to do somthing like this:
[~,~,mainraw] = xlsread(mainfilename,1);
unique_expense_types = unique(mainraw(:,2));
it should have goten all the cells into cell array type and then return full strings that are unique, though i receive this output:
ans =
' "'()-./01245689BEGILMORWY'
please help fixing it
Related
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.
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 }
Here is my code. I have 59 CSV files in my directory, I have 11 variables in each, with first one date in quarter format. I need to tell MATLAB that first column has date format, because the code below imports it as a string variable.
ext = '.csv';
countries = dir(['*', ext]);
countryFiles = {countries.name};
countriesNames = strrep(countryFiles, ext, '');
%%
a = cell(length(countriesNames), 2);
a(:,1) = countriesNames(:)';
a(:,2) = cellfun(#(file) readtable(file, 'TreatAsEmpty', '.','Format'?), countryFiles(:), 'uni', 0);
As far as I understand this is option 'Format' with datenum in readtable... However, I can`t find useful information on it in helpfiles and whatever I try I get errors... Below how my data looks for each country. Data is 1980Q1-2014Q4.
So I have 59*2 cell array with first column representing countryNames and second column contains 59 140*11 tables. First I do not know how to access variable names within cell array`s table. Second problem is that if you try to define x to a column in that table and then datenum(x,'YYYYQQ') I get "Input expected to be a cell array, was table instead."
So, I need to extract from cell a tables in a{1,2}, a{2,2}, a{3,2} ... then convert those tables to cellarray using table2cell option. How to do it using loop for each country and then write it back to the cell?
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.
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.