Use of arrayfun and sprintf to order data

1 回表示 (過去 30 日間)
Isma_gp
Isma_gp 2016 年 9 月 26 日
コメント済み: Guillaume 2016 年 9 月 27 日
Hi, I have a 1x4cell (files) with 1x1struct in each cell. Each structure contains several variables. I would like to extract from each structure only the variables called: SL_X_Y, where X can be numbers from 1 to 7 and Y could be numbers from 1 to 3 i.e. SL_5_2. Not all the combinations are found in each structure. The resulting 1x4cell will only contain structures with the variables SL_X_Y that are found.
Thanks
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2016 年 9 月 26 日
編集済み: Andrei Bobrov 2016 年 9 月 26 日
Attach your data.  
 
Isma_gp
Isma_gp 2016 年 9 月 27 日
An example file attached

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 9 月 27 日
編集済み: Guillaume 2016 年 9 月 27 日
1. Create a function to filter the relevant fields of the structure:
function filtered = filterstruct(unfiltered)
%FILTERSTRUCT: keep only the fields of the structure which match SL_X_Y with X integer between 1 and 7 and Y integer between 1 and 3
fn = fieldnames(unfiltered); %list of all fields
toremove = cellfun(@isempty, regexp(fn, '^SL_[1-7]_[1-3]$', 'once')); %which fields do not match?
filtered = rmfield(unfiltered, fn(toremove)); %get rid of non matching fields
end
2. Apply cellfun:
cellfun(@filterstruct, yourcellarray, 'UniformOutput', false)
Note however, that it appears you use the variable names to store metadata about the variable content, which is not a good idea and the reason you're struggling. Better would have been to have just one SL field which is a 7x3 cell array / matrix. Extraction would have been trivial then.
edit: typo in the regex
  4 件のコメント
Isma_gp
Isma_gp 2016 年 9 月 27 日
編集済み: Isma_gp 2016 年 9 月 27 日
I have some troubles to make it work. I'm attaching an example of the input file. I would like a 7x3 cell array containing the SLAM_SH10_X_Y variables in the right cell (X=row Y=column), for each of the four structures.
Thanks
Guillaume
Guillaume 2016 年 9 月 27 日
Of course, it doesn't work. In each function, the regular expression is looking for the pattern 'SL_X_Y' (as you originally stated), not 'SLAM_SH10_X_Y'. In addition, the X and Y limits don't match what you said (X goes up to 5, Y goes up to 7).
You need to change the regular expression accordingly.
This will do the filtering and transformation into a cell array. I've removed any restriction on the magnitude of X and Y. The cell array will be as big as necessary.
function filtered = filtertocell(unfiltered)
%FILTERTOCELL: filter a structure to only keep fields SLAM_SH10_X_Y and convert fields to 2D cell array
%X and Y are integers indicating the row, column respectively of the destination cell
validateattributes(unfiltered, {'struct'}, {'scalar', 'nonempty'});
fn = fieldnames(unfiltered);
rc = cellfun(@str2double, regexp(fn, '^SLAM_SH10_(\d+)_(\d+)$', 'tokens', 'once'), 'UniformOutput', false);
unshapedfiltered = struct2cell(rmfield(unfiltered, fn(cellfun(@isempty, rc))));
rc = vertcat(rc{:});
filtered = cell(max(rc));
filtered(sub2ind(size(filtered), rc(:, 1), rc(:, 2))) = unshapedfiltered;
end
cellfun(@filtertocell, files, 'UniformOutput', false)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by