下面的Matlab脚本创建了一个.mat文件。其具有多种情况。每个案例都有不同的输出。我多次运行这个脚本,并将输出手动存储在这样的excel表中。例如,在第一次运行脚本后,我将输出保存在Run1列下,第二次保存在Run2中。从Run 3开始,我希望使用matlab从.mat文件导出结果,而无需手动输入。
注意:唯一更改的值是每次运行的输出值。每次跑步的休息时间都是一样的。
%% Example of how the data is structured
% initialize testSetup
testSetup = struct( ...
'name', {'01_Case1', '01_Case2', '01_Case3' , ...
'02_Case1', '02_Case2','02_Case3','02_Case4' ...
'03_Case1','03_Case2','03_Case3',...
'04_Case1','04_Case2'}, ...
'rollno', {struct, struct, struct,struct, struct, struct,struct,...
struct, struct,struct, struct, struct},...
'subject', {struct, struct, struct,struct, struct, struct,struct,...
struct, struct,struct, struct, struct},...
'evaluation',{struct, struct, struct,struct, struct, struct,struct,...
struct, struct,struct, struct, struct});
%input the data
testSetup(1).evaluation.output1.result=0.1;
testSetup(1).evaluation.output2.result=0.2;
testSetup(1).evaluation.output3.result=0.3;
testSetup(2).evaluation.output1.result=0.1;
testSetup(2).evaluation.output2.result=0.2;
testSetup(2).evaluation.output3.result=0.3;
testSetup(2).evaluation.output4.result=0.4;
testSetup(3).evaluation.output1.result=0.1;
testSetup(3).evaluation.output2.result=0.2;
testSetup(3).evaluation.output3.result=0.3;
testSetup(4).evaluation.output1.result=0.1;
testSetup(4).evaluation.output2.result=0.2;
testSetup(5).evaluation.output1.result=0.1;
testSetup(5).evaluation.output2.result=0.2;
testSetup(5).evaluation.output3.result=0.3;
testSetup(6).evaluation.output1.result=0.1;
testSetup(6).evaluation.output2.result=0.2;
testSetup(6).evaluation.output3.result=0.3;
testSetup(7).evaluation.output1.result=0.1;
testSetup(7).evaluation.output2.result=0.2;
testSetup(7).evaluation.output3.result=0.3;
testSetup(8).evaluation.output1.result=0.1;
testSetup(8).evaluation.output2.result=0.2;
testSetup(9).evaluation.output1.result=0.1;
testSetup(9).evaluation.output2.result=0.2;
testSetup(9).evaluation.output3.result=0.3;
testSetup(10).evaluation.output1.result=0.1;
testSetup(10).evaluation.output2.result=0.2;
testSetup(11).evaluation.output1.result=0.1;
testSetup(11).evaluation.output2.result=0.2;
testSetup(11).evaluation.output3.result=0.3;
testSetup(12).evaluation.output1.result=0.1;
testSetup(12).evaluation.output2.result=0.2;
%save as .mat
save testsetup.mat
这里有两种方法可以做到这一点。
首先,让我们定义数据。从你的问题中还不能100%清楚你的数据是如何结构化的,所以你必须将其转换成这种格式。
header = {'Exp1', 'Exp2', 'Exp3'};
case1 = { ...
'class1', 90, 91;
'class2', 91, 91;
'class3', 92, 91;
'class4', 93, 91;
};
case2 = { ...
'class1', 90, 92;
'class2', 91, 92;
'class3', 92, 92;
};
case3 = { ...
'class1', 90, 93;
'class2', 91, 93;
'class3', 92, 93;
};
case4 = { ...
'class1', 90, 90;
'class2', 91, 90;
'class3', 92, 90;
};
第一种方法将每个组件分别写入工作表Sheet1
:
xlswrite('temp.xlsx', header, 'Sheet1', 'B1');
xlswrite('temp.xlsx', {'Case1'}, 'Sheet1', 'A2');
xlswrite('temp.xlsx', case1, 'Sheet1', 'A3');
xlswrite('temp.xlsx', {'Case2'}, 'Sheet1', 'A8');
xlswrite('temp.xlsx', case2, 'Sheet1', 'A9');
xlswrite('temp.xlsx', {'Case3'}, 'Sheet1', 'A13');
xlswrite('temp.xlsx', case3, 'Sheet1', 'A14');
xlswrite('temp.xlsx', {'Case4'}, 'Sheet1', 'A18');
xlswrite('temp.xlsx', case4, 'Sheet1', 'A19');
第二种方法在一次写入操作中将整个内容写入纸张Sheet2
,产生等效输出:
blank_row = repmat({NaN}, 1, 4);
C = cat(1, ...
cat(2, {NaN}, header), ...
{'Case1', NaN, NaN, NaN}, ...
cat(2, case1, repmat({NaN}, size(case1, 1), 1)), ...
blank_row, ...
{'Case2', NaN, NaN, NaN}, ...
cat(2, case2, repmat({NaN}, size(case2, 1), 1)), ...
blank_row, ...
{'Case3', NaN, NaN, NaN}, ...
cat(2, case3, repmat({NaN}, size(case3, 1), 1)), ...
blank_row, ...
{'Case4', NaN, NaN, NaN}, ...
cat(2, case4, repmat({NaN}, size(case4, 1), 1)));
xlswrite('temp.xlsx', C, 'Sheet2');
根据您提供的图片,您的数据结构如下(基于图片的示例数据(。当然,这不需要手动编码——testSetup
的实例将来自您的脚本,您只需要将它们附加到experiment
上。
如果您的数据结构不同,您可能需要对下面的代码进行一点调整。不过,这些变化应该不会太大。
%% Create sample data
% initialize testSetup
testSetup = struct( ...
'name', {'Case1', 'Case2', 'Case3'}, ...
'A', {struct, struct, struct}, ...
'B', {struct, struct, struct}, ...
'C', {struct, struct, struct});
% assume there are two experiments
experiment = struct('testSetup', {testSetup, testSetup});
% sample data for experiment 1
experiment(1).testSetup(1).C.class1.output = 90;
experiment(1).testSetup(1).C.class2.output = 91;
experiment(1).testSetup(1).C.class3.output = 92;
experiment(1).testSetup(1).C.class4.output = 93;
experiment(1).testSetup(2).C.class1.output = 90;
experiment(1).testSetup(2).C.class2.output = 91;
experiment(1).testSetup(2).C.class3.output = 92;
experiment(1).testSetup(3).C.class1.output = 90;
experiment(1).testSetup(3).C.class2.output = 91;
experiment(1).testSetup(3).C.class3.output = 92;
experiment(1).testSetup(4).C.class1.output = 90;
experiment(1).testSetup(4).C.class2.output = 91;
experiment(1).testSetup(4).C.class3.output = 92;
% sample data for experiment 2
experiment(2).testSetup(1).C.class1.output = 91;
experiment(2).testSetup(1).C.class2.output = 91;
experiment(2).testSetup(1).C.class3.output = 91;
experiment(2).testSetup(1).C.class4.output = 91;
experiment(2).testSetup(2).C.class1.output = 92;
experiment(2).testSetup(2).C.class2.output = 92;
experiment(2).testSetup(2).C.class3.output = 92;
experiment(2).testSetup(3).C.class1.output = 93;
experiment(2).testSetup(3).C.class2.output = 93;
experiment(2).testSetup(3).C.class3.output = 93;
experiment(2).testSetup(4).C.class1.output = 90;
experiment(2).testSetup(4).C.class2.output = 90;
experiment(2).testSetup(4).C.class3.output = 90;
现在我们有了数据,我们可以把它写到Excel中。我们的计划是将其写入一个单元阵列,一次一个实验。
该代码假设testSetup
对于每个实验都具有类似的结构。特别是,它假设每个实验的案例名称是相同的,并且在每个案例中的类是相同的。这两个假设在代码中得到了明确的检查。
%% Output to Excel
% CREATE CELL TO WRITE TO FILE (cell_to_write)
% initialize to overall header row
cell_to_write = cat(2, {NaN}, ...
cellfun(@(x)sprintf('Exp%d', x), num2cell(1 : length(experiment)), 'UniformOutput', false));
% names of cases
case_names = experiment(1).testSetup(casenum).name;
% ... check that each experiment has the same cases
for expnum = 1 : length(experiment)
if ~isequal(case_names, experiment(expnum).testSetup(casenum).name)
error('Cases are different for different experiments');
end
end
% create remainder of cell
for casenum = 1 : length(experiment(1).testSetup) % iterate over cases
% header row for current case
curr_header = cat(2, ...
{case_names}, ...
repmat({NaN}, 1, length(experiment)));
% classes within current case
curr_classes = fieldnames(experiment(1).testSetup(casenum).C);
% body for current case
% ... write names of classes
curr_body = cat(2, ...
curr_classes, ...
repmat({NaN}, length(curr_classes), length(experiment)));
% ... write numeric data
for expnum = 1 : length(experiment) % iterate over experiments
% check that each experiment has the same classes for the current case
if ~isequal(curr_classes, fieldnames(experiment(expnum).testSetup(casenum).C))
error('Classes are different for different experiments');
end
for classnum = 1 : length(curr_classes)
curr_body{classnum, expnum + 1} = ...
experiment(expnum).testSetup(casenum).C.(curr_classes{classnum}).output;
end
end
% append current case to cell
cell_to_write = cat(1, ...
cell_to_write, ...
curr_header, ... % current header
curr_body, ... % current body
repmat({NaN}, 1, size(cell_to_write, 2))); % blank row
end
% WRITE TO FILE
xlswrite('temp.xlsx', cell_to_write, 'Sheet1');
所以,我解决这个问题的方法是找到我的数据集(.mat文件(的大小,迭代案例的数量,访问每个案例的输出,然后将其转换为数组,以便可以轻松地将其复制到excel表中。
注意:如果存在大小写值为空的情况,则可以使用for循环来检查空值,并将其替换为NaN。
sz = size(testsetup,2);
exportArr=[];
% Converting to an array and transposing it to make it a column and later on copying
% it into an Excel Sheet.
for i = 1:sz
str = struct2array(testSetup(i).evaluation);
data = struct2array(str);
exportArr = [exportArr;nan;data'];
end