writetimetable을 사용하여 작업공간에 있는 여러 개의 테이블을 하나의 엑셀 파일에 작성
2 views (last 30 days)
Show older comments
MatlabFunction에서 writetimetable을 사용하여 작업공간에 있는 여러 개의 테이블을 하나의 엑셀 파일에 작성하려고 합니다.
table1 기록 후 바로 옆 셀부터 다시 table2를 기록하고 싶습니다.
S = size(Talbe2);
filename = 'TestResult.xlsx';
writetimetable(table1,filename,'Sheet',1,'Range', 'A1');
writetimetable(table2,filename,'Sheet',1,'Range', ???); %셀 위치를 A1+S(2)로 설정하려면 어떻게 해야하나요?
y = u;
0 Comments
Accepted Answer
Angelo Yeo
on 25 Jan 2024
Edited: Angelo Yeo
on 25 Jan 2024
아래의 코드를 활용하실 수 있을 것으로 생각됩니다.
%% Making dummy tables for demonstration
LastName = ["Sanchez";"Johnson";"Zhang";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Smoker = [true;false;true;false;true];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
table1 = table(LastName,Age,Smoker,Height,Weight,BloodPressure);
table2 = table1; % copying the same table for a demonstration purpose
%%
S = size(table1);
filename = 'TestResult.xlsx';
writetable(table1, filename, 'Sheet', 1, 'Range', 'A1');
mycol = num2col(S(2)+1);
writetable(table1, filename, 'Sheet', 1, 'Range', [mycol, '1']);
T = readtable(filename) % final result
function col = num2col(num)
% Source: https://stackoverflow.com/questions/14261648/convert-excel-column-number-to-column-name-in-matlab
% There are 26 kinds of alphabets.
remainder = mod(num - 1, 26) + 1;
num = num - remainder;
num = num/26;
% And, the column names are repetitively added in Excel.
if num > 0
col = append(num2col(num), char(64 + remainder));
else
col = char(64 + remainder);
end
end
5 Comments
Angelo Yeo
on 30 Jan 2024
Edited: Angelo Yeo
on 30 Jan 2024
MATLAB Function Block을 사용하는지 몰랐습니다. 이런 경우에는 재귀식으로 출력값을 점차적으로 바꿔가는 형식은 맞지 않을 것 같습니다. 새로운 모델을 공유드리니 확인해주십시오. MATLAB Function 블록 안의 내용은 아래와 같습니다.
function y = fcn(u)
coder.extrinsic("evalin");
coder.extrinsic("writetimetable");
coder.extrinsic("sRowRef");
coder.extrinsic("lRowRef");
%
% RowTimes = seconds(1:5)';
% TestCase = timetable(RowTimes,[98;97.5;97.9;98.1;97.9],[120;111;119;117;116],...
% 'VariableNames',{'Reading1','Reading2'})
T1 = evalin("base", "TestCase");
T2 = T1;
s = zeros(1, 2);
s = size(T1);
filename = 'TestResult.xlsx';
writetimetable(T1, filename, 'Sheet', 1, 'Range', 'A1');
mycol = let_loc(s(2)+2); % The width of time table does not count the rowTimes. Hence we should add 2 not 1.
writetimetable(T2, filename, 'Sheet', 1, 'Range', [mycol, '1'])
y=1;
end
function [col_str] = let_loc(num_loc)
test = 2;
old = 0;
x = 0;
while test >= 1
old = 26^x + old;
test = num_loc/old;
x = x + 1;
end
num_letters = x - 1;
str_array = zeros(1,num_letters);
for i = 1:num_letters
loc = floor(num_loc/(26^(num_letters-i)));
num_loc = num_loc - (loc*26^(num_letters-i));
str_array(i) = char(65 + (loc - 1));
end
col_str = strcat(str_array(1:length(str_array)));
end
More Answers (0)
See Also
Categories
Find more on 선형 예측 부호화 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!