writetimetable을 사용하여 작업공간에 있는 여러 개의 테이블을 하나의 엑셀 파일에 작성
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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;
채택된 답변
Angelo Yeo
2024년 1월 25일
편집: Angelo Yeo
2024년 1월 25일
1 개 추천
아래의 코드를 활용하실 수 있을 것으로 생각됩니다.
%% 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
eblee
2024년 1월 30일
function col을 추가하여 실행했더니 'All outputs must be assigned before any run-time recursive call.'라는 에러가 발생했습니다.
그래서 col에 값을 할당하는 코드를 아래와 같이 추가해주었습니다.
function col = num2col(num)
remainder = mod(num - 1, 26) + 1;
num = num - remainder;
num = num/26;
col = char(64+remainder); %추가한 코드
if num > 0
disp(num2col(num));
disp(char(64+remainder));
col = append(num2col(num), char(64 + remainder));
else
col = char(64 + remainder);
end
end
그러나
col = append(num2col(num), char(64 + remainder));
위 코드에서 Size mismatch(size [1 x 1] ~= size [1 x 2])라는 새로운 error가 발생하고 있습니다.
해결방법이 있을까요?
Angelo Yeo
2024년 1월 30일
@은비: 에러를 재현하기 위한 전체 코드를 올려주십시오. 지금 주신 코드만으로는 무엇이 문제인지 알 수 없습니다.
eblee
2024년 1월 30일
function y = fcn(u)
coder.extrinsic("evalin");
coder.extrinsic("writetimetable");
coder.extrinsic("sRowRef");
coder.extrinsic("lRowRef");
T1 = evalin("base", "TestCase");
T2 = T1;
s = zeros(1,2);
s = size(T1);
disp(s);
filename = 'TestResult.xlsx';
writetimetable(T1, filename, 'Sheet', 1, 'Range', 'A1');
mycol = zeros(0);
mycol = num2col(s(2)+1);
writetimetable(T2, filename, 'Sheet', 1, 'Range', [mycol, '1'])
y=1;
end
function col = num2col(num)
remainder = mod(num - 1, 26) + 1;
num = num - remainder;
num = num/26;
col = char(64+remainder, 64+remainder, 64+remainder);
if num > 0
disp(num2col(num));
disp(char(64+remainder));
col = append(num2col(num), char(64 + remainder));
else
col = char(64 + remainder);
end
end
전체 코드입니다.
Angelo Yeo
2024년 1월 30일
편집: Angelo Yeo
2024년 1월 30일
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
2024년 1월 31일
@Angelo Yeo 제대로 잘 동작합니다. 감사합니다!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 선형 예측 부호화에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)