concatenation - How to automatically name and concatenate cells from different operations in Matlab -


i wrote sample code illustrate problem - see below. have several operations, each executed independently (not 4 in example, more). want to...

1) automate naming of results can larger number of years, parts of years , plant types (e.g. name variable "string200811" when year = 2008, partofyear = 1, planttype = 1 etc.)

2) automate concatenation, (see below).

let me know if unclear!

% operation 1 year = 2008; partofyear = 1; planttype = 1; string200811 = 'blabla'; % random result number200811 = rand(1); % other random result vector200811 = [rand(1); rand(1); rand(1); rand(1)]; % other random result  % operation 2 year = 2008; partofyear = 1; planttype = 2; string200812 = 'blablablubb'; number200812 = rand(1); vector200812 = [rand(1); rand(1); rand(1); rand(1)];  % operation 3 year = 2008; partofyear = 2; planttype = 1; string200821 = 'blablabla'; number200821 = rand(1); vector200821 = [rand(1); rand(1); rand(1); rand(1)];  % operation 4 year = 2008; partofyear = 2; planttype = 2; string200822 = 'blablablablubb'; number200822 = rand(1); vector200822 = [rand(1); rand(1); rand(1); rand(1)];  % concatenate results results = {2008, 1, 1, string200811, number200811;...            2008, 1, 2, string200812, number200812;...            2008, 2, 1, string200821, number200821;...            2008, 2, 2, string200822, number200822} table = cell2table(results); writetable(table,'resultstest.xls','sheet',1);  vectors = vertcat(vector200811, vector200812, vector200821, vector200822) 

i think can achieve want using cellfun(func, c), which:

calls function specified function handle func , passes elements cell array c

so simple example be

 % cell array of vectors  c = {[1 2 3], [4 5 6], [7 8 9], [10 11 12]};  % "end" (last) element of each of cell array's members  output = cellfun(@(x) x(end), c);  >> output = [3 6 9 12] 

to make column vector instead of row vector, transpose it

 output = cellfun(@(x) x(end), c).'; % column vector output 

this "concatenating last elements of each set of cells".


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -