What is the best way to show a symbolic matrix in some tool in Guide?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Oscarin
el 18 de Ag. de 2018
Comentada: Walter Roberson
el 27 de Ag. de 2018
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194029/image.jpeg)
2.- result with some variables
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194030/image.jpeg)
3.- Matlab show this error
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194031/image.jpeg)
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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 18 de Ag. de 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.
B) Do not use dynamic variable names http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
C) symbolic variables are not numeric. You cannot use a %f format to display them.
10 comentarios
Walter Roberson
el 27 de Ag. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!