How to make multiple execution of a code by loop ?

조회 수: 2 (최근 30일)
Ivan Mich
Ivan Mich 2021년 4월 12일
댓글: Walter Roberson 2021년 4월 13일
Hello,
I have a code that uses one equation in order to take an output file. I use for my calculations randn so in each running of my code I have dofferent results. I would like to export for 100 executes of my code 100 outputs. I used this code
clc
clear
T=readtable('Outputresults.txt')
A=T(:,1)
B=T(:,2)
x=T(:,3)
x=table2array(x)
for i=1:100
y=randn +x
x=array2table(x)
y=array2table(y)
TABLE=[A B x y]
TABLE.Properties.VariableNames(1:6) = {'A','B','x','y'}
writetable(TABLE,['results' num2str(i) '.txt'],'Delimiter',' ')
end
but command window shows me error after the first execution.
The error is:
Undefined operator '+' for input arguments of type 'table'.
Could you please help me?

답변 (2개)

DGM
DGM 2021년 4월 13일
편집: DGM 2021년 4월 13일
Well, since I don't know what the error message was, then I'll just guess it was this.
T=readtable('Outputresults.txt')
A=T(:,1);
B=T(:,2);
xt=T(:,3);
x=table2array(xt); % you don't need to convert it back to a table a 100 times.
for i=1:100
y=randn+x;
y=array2table(y);
TABLE=[A B xt y]; % just use the table copy of x
TABLE.Properties.VariableNames(1:4) = {'A','B','x','y'}; % you were assigning 4 things to 6 elements
writetable(TABLE,['results' num2str(i) '.txt'],'Delimiter',' ');
end
  댓글 수: 1
Ivan Mich
Ivan Mich 2021년 4월 13일
편집: Ivan Mich 2021년 4월 13일
The error is:
Undefined operator '+' for input arguments of type 'table'.
Could you please help me?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2021년 4월 13일
Replace
x=array2table(x)
y=array2table(y)
TABLE=[A B x y];
TABLE.Properties.VariableNames(1:4) = {'A','B','x','y'};
with
TABLE = table(A, B, x, y);
  댓글 수: 3
Ivan Mich
Ivan Mich 2021년 4월 13일
command window shows me:
Error using writetable (line 155)
Writing nested tables/timetables is not supported. Use SPLITVARS on the nested table
before writing.
Error in code (line 52)
writetable(TABLE,['results' num2str(i) '.txt'],'Delimiter',' ')
Could you help me?
Walter Roberson
Walter Roberson 2021년 4월 13일
T = readtable('Outputresults.txt');
T.Properties.VariableNames{1} = 'A';
T.Properties.VariableNames{2} = 'B';
T.Properties.VariableNames{3} = 'x';
xt = T{:,3};
for i = 1:10
T.y = randn + xt;
writetable(TABLE,['results' num2str(i) '.txt'],'Delimiter',' ');
end
Questions:
  • are you sure you want to add a scalar random number each time, so you are adding the same number to each row of x? Or do you want to add a different random number to each row of x?
  • when you add a random number, are you wanting to each time be using the data read in as the base? Or are you wanting to accumulate random numbers, so that the second output ?
%like the above, but the random numbers accumulate. And different one for
%each row
T = readtable('Outputresults.txt');
T.Properties.VariableNames{1} = 'A';
T.Properties.VariableNames{2} = 'B';
T.Properties.VariableNames{3} = 'x';
xt = T{:,3};
nxt = length(xt);
for i = 1:10
xt = randn(nxt,1) + xt;
T.y = xt;
writetable(TABLE,['results' num2str(i) '.txt'],'Delimiter',' ');
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by