2017-07-25 37 views
-1

我写了一个示例代码来说明我的问题 - 请参见下文。我有几个操作,每个操作都是独立执行的(不仅在示例中为4个,而且更多)。我想...如何在Matlab中自动命名和连接来自不同操作的单元格

1)使结果的命名自动化,以便我可以在更长的年数,部分年份和植物类型上做到这一点(例如,在Year = 2008时命名变量“string200811” PartOfYear = 1,PlantType = 1等)

2)自动连接(见下文)。

让我知道如果有什么不清楚!

% 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) 
+6

请给出一个例子,说明“C”是什么以及你期望你的输出是什么。 “做相反的事情”并不是对你想要发生什么的非常明确的描述...... – Wolfie

+0

可能你需要'C {end}'。但是我们需要一个[MCVE]来理解你的问题到底是什么 –

+0

我相信这个问题被删除是一个错误。我的理解是它的原话是无关紧要的,但我完全改变了它,现在问题非常清楚,并且由于我发布的代码,答案可以证实。这就是为什么我会重发这个问题。 – LenaH

回答

1

我认为你可以实现你想要使用cellfun(func, C),这是什么:

调用由函数指定的函数处理func并将从单元阵列C

因此,一个简单的元素示例将是

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

为了使这一个列向量,而不是一个行向量,只需转置它

output = cellfun(@(x) x(end), C).'; % Column vector output 

这是“串联每个小区组的最后一个元件”。

+0

谢谢!这很有用,但是有几个人提出我的问题太广泛了,现在我发布了一个完整的代码,希望能够澄清我的具体问题。 – LenaH

相关问题