writetimetable을 사용하여 작업공간에 있는 여러 개의 테이블을 하나의 엑셀 파일에 작성
显示 更早的评论
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
아래의 코드를 활용하실 수 있을 것으로 생각됩니다.
%% 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 个评论
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가 발생하고 있습니다.
해결방법이 있을까요?
@은비: 에러를 재현하기 위한 전체 코드를 올려주십시오. 지금 주신 코드만으로는 무엇이 문제인지 알 수 없습니다.
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
@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)