I wrote a sample code to illustrate my problem - see below. I have several operations, where each is executed independently by a different server (not only 4 as in the example, but more). I want to...
1) Automate the naming of the results so that I can do it for a larger number of years, parts of years and plant types (e.g. name the variable "string200811" when Year = 2008, PartOfYear = 1, PlantType = 1 etc.)
2) Automate the concatenation, too (as in the concatenation in the code below).
Let me know if anything is unclear!
% Operation 1
Year = 2008;
PartOfYear = 1;
PlantType = 1;
string200811 = 'blabla'; % some random result
number200811 = rand(1); % some other random result
vector200811 = [rand(1); rand(1); rand(1); rand(1)]; % some 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)
It's unclear how you are initialising each "operation" and storing the results. You should just return some struct or cell array object.
Function to generate a struct:
% Operation x
function myStruct = createStruct()
% ... other functionality here which actually generates the data values
myStruct.Year = 2008;
myStruct.PartOfYear = 2;
myStruct.PlantType = 2;
myStruct.String = 'randomstring';
myStruct.Number = 0.5;
myStruct.Vector = 1:4;
end
Then put all of the structs in a cell array
% Generate structs and store in a cell array
myCellArray = cell(4,5);
myVectors = zeros(4,4);
for ii = 1:4
tempStruct = createStruct();
myCellArray{ii, 1} = tempStruct.Year;
myCellArray{ii, 2} = tempStruct.PartOfYear;
myCellArray{ii, 3} = tempStruct.PlantType;
myCellArray{ii, 4} = tempStruct.String;
myCellArray{ii, 5} = tempStruct.Number;
myVectors(ii,:) = tempStruct.Vector;
end
You could replace the createStruct
function with some createCellArray
function of a similar nature. This would shortcut having to assign the individual fields in the loop, but may make things less clear if your actual data is more complicated (with more fields to keep track of).
As a general rule, it's a bad idea to automatically name variables. You can do it (for instance by using the eval
function) but it's clunky, slow, and asking for debugging issues and the user losing track of variables. Storing things in generic structs or cell arrays is far better for usability and maintenance.