Saving the content of a cell array into a file
1 次查看(过去 30 天)
显示 更早的评论
I want to write a content of a cell array into an m-file. I copied the content of a function file and save it in a cell array so as to make some modifications and then save it back into another function file. I would then pass the same input to the two separate functions (original and modified). I have a number of the modified functions called msize. If the result of the original function is different from the modified, ONE is assigned to the output array that compares the result otherwise ZERO is assigned. The code is returning only ONEs or ZEROs. But when I executed the code step-wise, it's returning correct results. I don't know perhaps the writing I made to the new function file does not allow writing of other files so that the first result is often repeated. See the code below:
function [killVector] = Tester(fname,mutants,tcase)
%%An edited version of an original function is called its mutant.
% Test cases are generated and passed as "tcase" in this function while
% fname is the name of the function to be passed, mutants is the cell
% array of the mutants. The same test cases are passed to the original
% and each of its mutants. If the result of the original version is
% different from the mutant's, then the mutant is killed else alive. The
% killVector stores the vector of killing of each mutant. If the mutant is
% killed, its killing value is ONE but if it's alive, the value is ZERO
num = numel(de2bi(numOfLines(fname)));
msize=length(mutants);
a=bin2dec(num2str(tcase(1)))+1;
b=bin2dec(num2str(tcase(2:5)))+1;
c=bin2dec(num2str(tcase(6:end)))+1;
%
%disp(realResult);
nfname='mutantfname.m';
T=zeros(msize,1);
values=zeros(msize,1);
i = 1;
while(i <= msize/3)
%for i=1:msize
chr=mutants{i};
op=bin2dec(num2str(chr(1)))+1;
lnum=round(bin2dec(num2str(chr(2:num+1)))+1);
choice=round(bin2dec(num2str(chr(num+2:end)))+1);
if (op==1)
mutated=arithRep(fname,lnum,choice);
elseif (op==2)
mutated=relRep(fname,lnum,choice);
end
writeToFile(nfname,mutated);
% filenames=strcat('m',num2str(i),'.m');
% writeToFile(filenames,mutated);
mresult=mutantfname(a,b,c);
% delete('mutantfname.m');
% clear mutated;
values(i)=mresult;
r=randi(1);
realResult=sumOfRoots(a,b,c);% Try to shift it inside the loop after write
if isequal(realResult,mresult)
y=0;
else
y=1;
end
T(i)=y;
i = i + 1;
end
%killVector=T';
killVector=values';
end
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!