Use of arrayfun and sprintf to order data
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Isma_gp
am 26 Sep. 2016
Kommentiert: Guillaume
am 27 Sep. 2016
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 Kommentare
Akzeptierte Antwort
Guillaume
am 27 Sep. 2016
Bearbeitet: Guillaume
am 27 Sep. 2016
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 Kommentare
Guillaume
am 27 Sep. 2016
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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!