Text directly to Matlab code
10 views (last 30 days)
Show older comments
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;
Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!