Text directly to Matlab code

조회 수: 7 (최근 30일)
onenightin
onenightin 2016년 1월 10일
댓글: Stephen23 2016년 1월 10일
Hi, I was wondering if I have some text, they can be read from file or created within Matlab, can I possibly turn them directly to Matlab code, for example, I have a text
'a+b'
%And I want to assign the expression in this text to a variable, is it possible,
c=a+b
%Or if there is some alternative ways, thank you very much
A more complex version is as follows, where I want to generate a bunch of expression using loop
fprintf(file,'[');
flag=0;%This flag determines whether this is the first time th condition is satisfied.
for i=1:size_con
%Traverse through every Qc and lc
for row=1:size_con
for col=1:size_con
if(Qc{i}(row,col)~=0)
if(flag>0)
fprintf(file,'+');
end
flag=1;
if(Qc{i}(row,col)==1)
fprintf(file,'x%d^2',row);
else
fprintf(file,'x%d*x%d',row,col);
end
end
end
end
if(i~=size_con)
fprintf(file,';');
end
end
fprintf(file,']');
fclose(file);
Then I will obtain a array like
[x1^2+x1*x4+x2^2+x2*x5+x3^2+x3*x6+x4*x1+x4^2+x5*x2+x5^2+x6*x3+x6^2,...
Then I will assign this to
nlcon=x1^2+x1*x4+x2^2+x2*x5+x3^2+x3*x6+x4*x1+x4^2+x5*x2+x5^2+x6*x3+x6^2;

답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by