after command bottom, I want X=[d2;d4;d9]

6 views (last 30 days)
Reza
Reza on 28 Oct 2014
Edited: James Tursa on 29 Oct 2014
clc
clear
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s)
end
  10 Comments
James Tursa
James Tursa on 28 Oct 2014
@Geoff: Not sure of Tatina's reason, of course, but I have done this many times myself. E.g., to pass some symbolic matrices & vectors through a section of code to verify the resulting formulas match expected model. Etc.
Geoff Hayes
Geoff Hayes on 29 Oct 2014
@James - thanks for the clarification. I've never used the Symbolic Toolbox so wasn't sure if you this was something that is expected/typical.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 28 Oct 2014
Is this what you want? Basically repeated your code, but get rid of the zeros initialization which will make X a double array (not what you want):
clc
clear
% X=zeros(3,1) % Commented out
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s);
X(i,1)=s; % Build up symbolic matrix
end
  3 Comments
James Tursa
James Tursa on 28 Oct 2014
Edited: James Tursa on 28 Oct 2014
Sorry about that. I don't have MATLAB on the machine I am using right now, so can't verify the syntax. Try either X(i) = s, or pre-allocate X=[] and then use X=[X;s]. I know there is a way to do this because I have done it before. Could always build up a char array of the result and then eval it, I suppose.
James Tursa
James Tursa on 29 Oct 2014
Edited: James Tursa on 29 Oct 2014
Another attempt to get the symbolic vector defined in your code (still not on a machine w/MATLAB, so this is untested)
clc
clear
y='X=['; % Used to build up the symbolic vector string
A=[2;4;9];
n=length(A);
for i=1:n
s = ['syms d' int2str(A(i))];
eval(s);
y = [y 'd' int2str(A(i)) ';']; % Build up symbolic string
end
y(end) = ']'; % Replace the last ; with ]
eval(y); % Generate the symbolic vector

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!