Filter löschen
Filter löschen

writetimetable을 사용하여 작업공간에 있는 여러 개의 테이블을 하나의 엑셀 파일에 작성

4 Ansichten (letzte 30 Tage)
eblee
eblee am 25 Jan. 2024
Kommentiert: eblee am 31 Jan. 2024
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;

Akzeptierte Antwort

Angelo Yeo
Angelo Yeo am 25 Jan. 2024
Bearbeitet: Angelo Yeo am 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
T = 5×13 table
LastName Age Smoker Height Weight BloodPressure_1 LastName_1 Age_1 Smoker_1 Height_1 Weight_1 BloodPressure_1_1 BloodPressure_2 ___________ ___ ______ ______ ______ _______________ ___________ _____ ________ ________ ________ _________________ _______________ {'Sanchez'} 38 true 71 176 124 {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 {'Johnson'} 43 false 69 163 109 77 {'Zhang' } 38 true 64 131 125 {'Zhang' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 {'Brown' } 49 true 64 119 122 80
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 Kommentare
Angelo Yeo
Angelo Yeo am 30 Jan. 2024
Bearbeitet: Angelo Yeo am 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
eblee
eblee am 31 Jan. 2024
@Angelo Yeo 제대로 잘 동작합니다. 감사합니다!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!