Make a MatLab table out of 2 Cell Array - matlab

What is the most efficient way to generate a table from 2 cell arrays. Array A contains data and Array B contains the corresponding names. I would like to combine them into a convenient table structure.
Cell_Array_A =
{[10x10],[10x10],[];
[10x11],[],[10x12];
[9x10],[13x10],[]}
Cell_Array_B =
{['A','B',[];
'B',[],'A';
'B','A',[]}
It should generate a table with the headers 'A' and 'B'. However, in my real data set, I have a lot of variable names, which I don't know. So I need a way to read all variables from the first row and use them to create the table for the rest of the matrix.
Example of the desired output:
'A' 'B'
[10x10] [10x10]
[10x12] [10x11]
[13x10] [9x10]
I tried so far to process the arrays row wise to get rid of the empty arrays. However this is not very efficient. I run the following code for each row in Array A and B. Below an example for the first row in Array A which I later on use as the tablet for my table.
order_row = {};
order_row = Cell_Array_A(1,:);
ordered_filenames = order_row(~cellfun('isempty',order_row));

Edited answer The idea is 1) to collect the non-empty column headers, 2) for each unique column header collect the corresponding values:
% Unique variable names
nul_names = cellfun(#isempty,Cell_Array_B);
var_names = unique(Cell_Array_B(~nul_names));
% Function to find a column header index
ixhead = #(h,c) cellfun(#(x) isequaln(x,h), c(:));
% Collect the values
var_values = cellfun( ...
#(h) Cell_Array_A(ixhead(h, Cell_Array_B)), var_names, ...
'UniformOutput', false ...
);
% Create the table
tbl = table(var_values{:}, 'VariableNames', var_names);

Related

Concatenating (sub)fields of structs in a cell array

I have a Matlab object, that is a cell array containting structs that have almost identical structures and I want to programmatically get a (sub)field of the structs of all cell array elements.
For example, we take test
test = {struct('a',struct('sub',1)), struct('a',struct('sub',2),'b',1)};
This will create a cell array with the following structure:
cell-element 1: a --> sub --> 1
cell-element 2: a --> sub --> 2
\-> b --> 1
It can be seen that the elements of test don't have the exact same structure, but similar. How can I get all values of the a.sub fields of the elements of the cell. I can obtain them in this specific problem with
acc=zeros(1,numel(test));
for ii=1:numel(test)
acc(ii) = test{ii}.a.sub;
end
but I can't quite get this method to work in a more general context (ie. having different fields).
You may want to use the function getfield:
%//Data to play with
test = {struct('a',struct('sub',1)), struct('a',struct('sub',2),'b',1)};
%//I'm interested in these nested fields
nested_fields = {'a', 'sub'};
%//Scan the cell array to retrieve the data array
acca = cellfun(#(x) getfield(x, nested_fields{:}), test);
In case your data cannot guarantee that all the elements are the same type and size, then you need to output a cell array instead:
%//Scan the cell array to retrieve the data cell array
accc = cellfun(#(x) getfield(x, nested_fields{:}), test, 'UniformOutput', false);
Later Edit
If one wants to use different sets of nested fields for each cell element then:
%//nested_fields list should have the same size as test
nested_fields = {{'a','sub'}, {'b'}};
accm = cellfun(#(x,y) getfield(x,y{:}), test, nested_fields, 'UniformOutput', false);
Edit: No need for recursion, as shown by #CST-link:s answer; the native getfield function can neatly unfold a cell array of fields as its second argument, e.g. getfield(foo{i}, fields{:}) instead of the call to the recursive function in my old answer below. I'll leave the recursive solution below, however, as it could have some value in the context of the question.
You can build you own recursive version of getField, taking a cell array of fields.
function value = getFieldRec(S,fields)
if numel(fields) == 1
value = getfield(S, fields{1});
else
S = getfield(S,fields{1})
fields{1} = [];
fields = fields(~cellfun('isempty',fields));
value = getFieldRec(S,fields);
end
end
Example usage:
foo = {struct('a',struct('sub',1)), ...
struct('a',struct('sub',2),'b',3), ...
struct('c',struct('bar',7),'u',5)};
accessFields = {'a.sub', 'b', 'c.bar'};
values = zeros(1,numel(foo));
for i = 1:numel(foo)
fields = strsplit(accessFields{i},'.');
values(i) = getFieldRec(foo{i},fields);
end
With the following result
values =
1 3 7
I have found a way to do this using eval:
function out = catCellStructSubField(cellStruct, fieldName)
out = zeros(1,numel(cellStruct));
for ii = 1:numel(cellStruct)
out(ii) = eval(['cellStruct{ii}.' fieldName]);
end
Where it can be used on my test example like this:
catCellStructSubField(test, 'a.sub')
Dynamic field names (cellStruct{ii}.(fieldName)) does not work because I'm accessing sub-fields.
I know eval is often a bad idea. I'm curious for different solutions.

Correct way of adding to a list from cell array in matlab?

I'm writing some code to split up a 180x2 matlab cell array based on a string in the second column. This string is one of EP,GA,PS,SS or SA. In python I could define empty lists then use conditionals to iterate over the elements of the list and append them to the relevant lists.
The Code
EP=[];
GA=[];
PS=[];
SA=[];
SS=[];
for i=1:size(d),
if strcmp(d(i,2),'EP'),
append(EP,d(i,1))
elseif strcmp(d(i,2),'GA'),
append(GA,i)
elseif strcmp(d(i,2),'PS'),
append(PS,i)
elseif strcmp(d(i,2),'SA'),
append(SA,i)
elseif strcmp(d(i,2),'SS'),
append(SS,i)
end
end
Note that 'd' is a 180x2 cell array that I copied and pasted into matlab rather than import. The general structure of the data however is:
12.9089000000000 'EP'
13.3697000000000 'SA'
13.4335000000000 'EP'
13.5302000000000 'PS'
13.8434000000000 'EP'
14.2583000000000 'EP'
14.8221000000000 'GA'
However when attempting this tactic in matlab I get an error:
Error using append (line 38)
Wrong number of input arguments for obsolete
matrix-based syntax.
Error in Boxplot_All_results (line 12)
append(GA,i)
Could somebody tell me the correct way of doing this in matlab
First of all, please not that d is a cell array. To index the elements of a cell array, use {}. If you index using () like you did, you end up with a small cell array containing only the indexed element(s).
To append in Matlab you basically have two choices:
%concatenate the list with a scalar. Also suitable for two lists.
EP=[EP,d{i,1}] %could also be done using cat
%append to the end
EP(end+1)=d{i,1}
While this fixes the problem, I recommend to implement it on a more generic way:
names={'EP','GA','PS','SA','SS'}
s=struct()
for idx=1:numel(names)
s.(names{idx})=[d{strcmpi(d(:,2),names{idx}),1}]
end
You end up with a struct containing the data you want.
Can you show MatLab code?
How do you append those values to cell array?
You can just create one a = {}
and then append element at the size+1 index. That operation extends your cell array.
Or you can also do a trick like that: a = [a; {value}]
To append elements to a cell array, a correct syntax would be:
for i=1:size(d),
if strcmp(d(i,2),'EP'),
EP = [EP ; d(i,1) ] ; %// append(EP,d(i,1))
elseif strcmp(d(i,2),'GA'),
GA = [GA ; d(i,1) ] ; %// append(GA,i)
but there are more ways, as you can read in the documentation: Add Cells to a Cell Array
There are also more ways to build your final extracted arrays in one assignment instead of having them growing dynamically (mLint will complain about that by the way).
Get the indices of the elements which satisfy your condition then create an array with only the matching elements. For example:
iEP = cellfun( #(c) strcmp(c,'EP') , d(:,2) ) ; %// logical array of indexes where the condition is true
EP = d(iEP,1) ; %// Create "EP" in one assignment - EP is a [cell] array
If you only have numeric values to retrieve in these new variables, may be having a double array instead of a cell array will be convenient :
iGA = cellfun( #(c) strcmp(c,'GA') , d(:,2) ) ; %// logical array of indexes where the condition is true
GA = cell2mat( d(iGA,1) ) ; %// Create "GA" in one assignment - GA is a [double] array
Of course you can bypass the intermediate variable holding the indices:
PS = d( cellfun(#(c)strcmp(c,'PS'),d(:,2)),1) ; %// Create "PS" in one assignment [cell] array
SA = cell2mat(d(cellfun(#(c)strcmp(c,'SA'),d(:,2)),1)) ; %// Create "SA" in one assignment [double] array

Function for comparing two cell arrays

I have a cell array (2000*10) with each cell containing a string such as '25:20:55'.
I want to write a function that accepts 10 inputs (say '25:02:33', '58:69:88', '25:54:96', '48:58:36', '58:54:88' and so on) and looks for a match in each column corresponding to input value for that particular column (i.e. the first input data corresponds to 1st column, 2nd to 2nd column of the stored data and so on.
How can I write a function for the above comparison?
Assuming the cell array contains strings, you can find matches using strcmp:
input = {'25:02:33', '58:69:88', '25:54:96', '48:58:36', '58:54:88'};
match_idx = strcmp(repmat(input, size(cell_data,1),1), cell_data);
If you only want to know if there are matches in the respective columns at all and do not care about the line index of the match, you can do
match = any(match_idx,1);
Use ismember
ismember(your_var, your_cell);
e.g.
c = {'a', 'b', 'c', 'd'};
ismember('b', c)
ans =
1
ismember('q', c)
ans =
0
In your case, something like could work
function arr = myfun(inputvec, inputmat)
for i=1:length(inputvec) %// where inputvec is the cell-vector with the strings you want to search for
arr(i) = ismember(inputvec{i}, inputmat{:,i});
end

assigning values to a field of an structure array in MATLAB

I want to replace the value of the fields in a structure array. For example, I want to replace all 1's with 3's in the following construction.
a(1).b = 1;
a(2).b = 2;
a(3).b = 1;
a([a.b] == 1).b = 3; % This doesn't work and spits out:
% "Insufficient outputs from right hand side to satisfy comma separated
% list expansion on left hand side. Missing [] are the most likely cause."
Is there an easy syntax for this? I want to avoid ugly for loops for such simple operation.
Credits go to #Slayton, but you actually can do the same thing for assigning values too, using deal:
[a([a.b]==1).b]=deal(3)
So breakdown:
[a.b]
retrieves all b fields of the array a and puts this comma-separated-list in an array.
a([a.b]==1)
uses logical indexing to index only the elements of a that satisfy the constraint. Subsequently the full command above assigns the value 3 to all elements of the resulting comma-separated-list according to this.
You can retrieve that the value of a field for each struct in an array using cell notation.
bVals = {a.b};
bVals = cell2mat( bVals );
AFAIK, you can't do the same thing for inserting values into an array of structs. You'll have to use a loop.

Simultaneously assign values to multiple structure fields

I have a matlab structure that follows the following pattern:
S.field1.data1
...
.field1.dataN
...
.fieldM.data1
...
.fieldM.dataN
I would like to assign values to one data field (say, data3) from all fields simultaneously. That would be semantically similar to:
S.*.data3 = value
Where the wildcard "*" represents all fields (field1,...,fieldM) in the structure. Is this something that can be done without a loop in matlab?
Since field1 .. fieldM are structure arrays with identical fields, why not make a struct array for "field"? Then you can easily set all "data" members to a specific value using deal.
field(1).data1 = 1;
field(1).data2 = 2;
field(2).data1 = 3;
field(2).data2 = 4;
[field.data1] = deal(5);
disp([field.data1]);
A loop-based solution can be flexible and easily readable:
names = strtrim(cellstr( num2str((1:5)','field%d') )); %'# field1,field2,...
values = num2cell(1:5); %# any values you want
S = struct();
for i=1:numel(names)
S.(names{i}).data3 = values{i};
end
In simple cases, you could do that by converting your struct into a cell array using struct2cell(). As you have a nested structure, I don't think that will work here.
On the other side, is there any reason why your data is structured like this. Your description gives the impression that a simple MxN array or cell array would be more suitable.