What is the best way to show a symbolic matrix in some tool in Guide?

18 views (last 30 days)
Hello my friends.
I am developing a graphic interface in Guide, where I have to use matrix with variables, in this time I fill the data in a table, after of process I try to shows the final matrix in a listbox but if matrix has integer numbers, matrix is shown but if matrix has variables matlab show the next mistake.
1.- result with only integer numbers
2.- result with some variables
3.- Matlab show this error
this is my code "Resolver" Button
if true
filas = str2double(get(handles.eslabones,'String'));
filas=filas-1;
F = sym('A', [filas 4]);
B = eye(4);
C = sym('C', [4 4]);
A=0;
A=cell2mat(get(handles.uitable1,'Data'));
med = str2double(get(handles.gradrad,'String'));
if med == 2
for i=1:filas
c1=(A(i,1))
c2=(A(i,2))
c3=(A(i,3))
c4=(A(i,4))
F(i,1)=c1;
F(i,2)=c2;
F(i,3)=c3;
F(i,4)=c4;
C=simplify([cos(F(i,4)) -sin(F(i,4))*cos(F(i,2)) sin(F(i,4))*sin(F(i,2)) F(i,1)*cos(F(i,4));
sin(F(i,4)) cos(F(i,4))*cos(F(i,2)) -cos(F(i,4))*sin(F(i,2)) F(i,1)*sin(F(i,4));
0 sin(F(i,2)) cos(F(i,2)) F(i,3);
0 0 0 1]);
eval(sprintf('A%d = C;',i));
B=B*C;
eval(sprintf('A%d',i))
end
else
for i=1:filas
c1=(A(i,1))
c2=(A(i,2))
c3=(A(i,3))
c4=(A(i,4))
F(i,1)=c1;
F(i,2)=c2;
aux1=degtorad(F(i,2))
F(i,2)=aux1
F(i,3)=c3;
F(i,4)=c4;
aux1=degtorad(F(i,4))
F(i,4)=aux1
C=simplify([cos(F(i,4)) -sin(F(i,4))*cos(F(i,2)) sin(F(i,4))*sin(F(i,2)) F(i,1)*cos(F(i,4));
sin(F(i,4)) cos(F(i,4))*cos(F(i,2)) -cos(F(i,4))*sin(F(i,2)) F(i,1)*sin(F(i,4));
0 sin(F(i,2)) cos(F(i,2)) F(i,3);
0 0 0 1]);
eval(sprintf('A%d = C|;',i));
B=B*C;
eval(sprintf('A%d',i))
end
end
texto=[ ];
for k=1:4
texto1=sprintf(' %2.0f ',B(k,:));
texto=sprintf('%s %s \n',texto,texto1);
end
set(handles.resultado,'String',texto);
end
4.- this code part is where I try to show in a list box.
if true
texto=[ ];
for k=1:4
texto1=sprintf(' %2.0f ',B(k,:));
texto=sprintf('%s %s \n',texto,texto1);
end
set(handles.resultado,'String',texto);
end
thanks I hope your answer.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Aug 2018
A) Never use eval() with a symbolic expression. symbolic expressions are displayed in a language which is not MATLAB itself and is not MuPAD either, so eval() of them can produce errors or wrong results.
C) symbolic variables are not numeric. You cannot use a %f format to display them.
  10 Comments
Oscarin
Oscarin on 25 Aug 2018
Edited: Oscarin on 27 Aug 2018
thank you very much I have solved the error and I have another question, is there some book or where can I find that parts of code?
Thanks
Walter Roberson
Walter Roberson on 27 Aug 2018
Not really, the above comes from experience. Things like sprintf() and cellfun() and arrayfun() would be in most beginning MATLAB materials.
The general technique of computing a format and using the format later is common but might not happen to be in an introductory work.
The technique of putting data elements into a cell array and expanding the cell with {:} in a sprintf() or fprintf() call gets to be a bit MATLAB (and clones) specific. In this particular case I use it only to pass in a whole bunch of character vectors because sprintf() and fprintf() do not happen to be programmed to be able to look inside a cell array. However, it can also be used in more complicated situations involving mixed data types. When you start wanting to print out multiple columns of mixed data types, your two choices are to loop row by row passing in scalars appropriate for that row -- or using the more complicated approach of building up a cell and using {:} cell expansion to build up a list of arguments. The basic use to get a cell array of character vectors formatted is practically an example you might use to illustrate the idea of cell expansion (which would be discussed in basic MATLAB references, but not necessarily in short tutorials about using MATLAB.) However, the use to build up elaborate cells for formatting multiple columns is Intermediate level rather than introductory.
regexp() is not something I would consider introductory programming. If I recall, back when I went to university, the formal study of regular expressions was part of a 4th year course on computing theory, but it might possibly have been 3rd year instead, part of the compiler construction material. I would assess regular expressions is a topic that could be introduced in first year courses, just probably not as a first programming course -- and when I say introduced, I wouldn't want to get into the theory of how it works and what could or could not be computed with regular expressions.
On the other hand, the regexp() call could be replaced with
outstr = strsplit(sprintf(fmt, Mc{:}),'\n');
as this is a very elementary use of regexp() that is basically just string matching.
All in all... if I were to rewrite the above code for tutorial purposes for beginning MATLAB use, I would probably do so by looping line by line of output. The way I wrote the code was a more advanced version written the way it was in order to avoid explicit loops.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!