首页 > 解决方案 > 每次在 MATLAB 中运行代码时,如何更新下一列中我的 Excel 工作表中的计算数据?

问题描述

function []= process(f1, f2, f3, f4, height, th)

%%omitted the the large code from in between in order to just propose the problem%% 

ValuesInInches(12)=t1*t;
ValuesInInches(8)=realneck(f1,f2,height,th);
ValuesInInches(14)=t3*t; 
ValuesInInches(7)=t4*t;
ValuesInInches(11)= ValuesInInches(7);
ValuesInInches(5)=t5*t;
ValuesInInches(4)=t6*t;
ValuesInInches(6)=t7*t;
ValuesInInches(10)=t8*t;
ValuesInInches(9)=t9*t;
ValuesInInches(3)=t9*t1;
ValuesInInches(1)=t*t10;
ValuesInInches(2)=t11*t;
ValuesInInches(13)= measureknee(a5,t);
ValuesInInches=ValuesInInches';

file='measure.csv';

measurements{1,1}='Shirt Length';
measurements{2,1}='Full Shoulders';
measurements{3,1}='Sleeves';
measurements{4,1}='Muscle';
measurements{5,1}='Chest';
measurements{6,1}='Stomach';
measurements{7,1}='Hip';
measurements{8,1}='Neck';
measurements{9,1}='Trouser length';
measurements{10,1}='Trouser waist';
measurements{11,1}='Trouser hip';
measurements{12,1}='Thigh';
measurements{13,1}='Knee';
measurements{14,1}='Inseam';

T= table(measurements,ValuesInInches);
writetable(T,file);

end

每次更改代码时,我都必须更新文件 measure.csv 以创建值数据集。我能够将数据写入文件,但现在我需要当我的代码第二次运行时,它将计算的数据写入下一列,同时保持旧数据的安全。我的代码要么覆盖同一列中的数据,要么我每次都必须手动输入特定的行列交叉位置。

我的代码每次运行时都会计算 14 个值。请告诉我一种方法,以便我可以在我的父函数中使用它来提高整个过程的效率。

标签: matlabmatlab-compiler

解决方案


首先,如果您正在编写 Excel 表格,我宁愿建议您使用该函数xlswrite(filename,Data,sheet,pos),它允许您将值、向量或矩阵专门设置到您想要的工作表中的特定位置。

第二:如果你可以使用一个计数器来跟踪你写的行数,你可以这样做:

function []= process(f1, f2, f3, f4, height, th, counter)
% Your code...
% suppose you want to write ValuesInInches into the next row

strCounter = ('A':'Z'); % = 'ABC...Z'
strPos = [strCounter(counter) '1']; % = 'B1' for instance
xlswrite(file,ValuesInInches,strPos);

end

我希望这有帮助。


推荐阅读