Borrar filtros
Borrar filtros

how to tell matlab in a matrix that if my program does not assign value to an element do not fill it with 0?

1 visualización (últimos 30 días)
I'm doing a program in matlab with the gui, which is gauss jordan at the beginning my program is asking for the elements of the matrix but my program can store up to 5x6 then when you enter a 2x3 matrix the others fill it with 0 which I get an error when it comes to giving me results How can I tell him not to fill it with 0?
  3 comentarios
Erwin Avendaño
Erwin Avendaño el 2 de Nov. de 2017
Editada: Walter Roberson el 2 de Nov. de 2017
function pushbutton6_Callback(hObject, eventdata, handles)
m=[str2double(get(handles.edit3,'string')) str2double(get(handles.edit4,'string')) str2double(get(handles.edit6,'string')) (str2double(get(handles.edit9,'string'))); str2double(get(handles.edit10,'string')) str2double(get(handles.edit11,'string')) str2double(get(handles.edit12,'string')) (str2double(get(handles.edit15,'string'))); str2double(get(handles.edit16,'string')) str2double(get(handles.edit17,'string')) str2double(get(handles.edit18,'string')) str2double(get(handles.edit21,'string')) ]
if all(size(m)==[2,3])
h=1
else
if all(size(m)==[3,4])
h=2
else
if all(size(m)==[4,5])
h=3
else
if all(size(m)==[5,6])
h=4
end
end
end
end
switch h
case 1
m(1,:)=m(1,:)/m(1,1);
m(2,:)=m(2,:)-(m(2,1))*(m(1,:));
m(2,:)=m(2,:)/m(2,2);
m(1,:)=m(1,:)-(m(1,2))*(m(2,:));
x1=m(1,3)
x2=m(2,3)
set(handles.edit35,'string',x1);
set(handles.edit36,'string',x2);
case 2
m(1,:)=m(1,:)/m(1,1);
m(2,:)=m(2,:)-(m(2,1))*(m(1,:));
m(3,:)=m(3,:)-(m(3,1))*(m(1,:));
m(2,:)=m(2,:)/m(2,2);
m(3,:)=m(3,:)-(m(3,2))*(m(2,:));
m(3,:)=m(3,:)/m(3,3);
m(2,:)=m(2,:)-(m(2,3))*(m(3,:));
m(1,:)=m(1,:)-(m(1,3))*(m(3,:));
m(1,:)=m(1,:)-(m(1,2))*(m(2,:));
x1=m(1,4)
x2=m(2,4)
x3=m(3,4)
set(handles.edit35,'string',x1);
set(handles.edit36,'string',x2);
set(handles.edit37,'string',x3);
case 3
x=3
case 4
x=2
otherwise
x=32
end
Erwin Avendaño
Erwin Avendaño el 2 de Nov. de 2017
If you look at my program it fills the elements of the matrix as the user enters it in the matlab gui, they are the edittext but my program waits until 3x4 (at the moment I will do it later for 5x6) if the user enters a matrix of 2x3 the rest matlab fills it with 0 then when starting the program, makes the gauss jordan with a 3x4 matrix but fills the rest with 0

Iniciar sesión para comentar.

Respuesta aceptada

Erwin Avendaño
Erwin Avendaño el 3 de Nov. de 2017
Editada: Stephen23 el 3 de Nov. de 2017
thanks for answering but I solved my problem, when the user presses 2x2 what pushbutton does is declare as symbolic variable au, assign the value of u and save it with setappdata and so with the other buttons, u = 2, u = 3 etc . When you press the solve button I declare the symbolic variable u and I get the value of u according to the point that you pressed with getappdata. next I show you the code
function 2x2_Callback(hObject, eventdata, handles)
syms u
u=1
setappdata (0, 'evalue', u);
function solvethesystem_Callback(hObject, eventdata, handles)
syms u
u = getappdata (0, 'evalue');
switch u
case 1
m=[str2double(get(handles.edit3,'string')) str2double(get(handles.edit4,'string')) (str2double(get(handles.edit9,'string'))); str2double(get(handles.edit10,'string')) str2double(get(handles.edit11,'string')) str2double(get(handles.edit15,'string'))]
case 2
m=[str2double(get(handles.edit3,'string')) str2double(get(handles.edit4,'string')) str2double(get(handles.edit6,'string')) (str2double(get(handles.edit9,'string'))); str2double(get(handles.edit10,'string')) str2double(get(handles.edit11,'string')) str2double(get(handles.edit12,'string')) (str2double(get(handles.edit15,'string'))); str2double(get(handles.edit16,'string')) str2double(get(handles.edit17,'string')) str2double(get(handles.edit18,'string')) str2double(get(handles.edit21,'string')) ]
case 3
m=[str2double(get(handles.edit3,'string')) str2double(get(handles.edit4,'string')) str2double(get(handles.edit6,'string')) str2double(get(handles.edit7,'string')) (str2double(get(handles.edit9,'string'))); str2double(get(handles.edit10,'string')) str2double(get(handles.edit11,'string')) str2double(get(handles.edit12,'string')) str2double(get(handles.edit13,'string')) (str2double(get(handles.edit15,'string'))); str2double(get(handles.edit16,'string')) str2double(get(handles.edit17,'string')) str2double(get(handles.edit18,'string')) str2double(get(handles.edit19,'string')) (str2double(get(handles.edit21,'string'))); str2double(get(handles.edit22,'string')) str2double(get(handles.edit23,'string')) str2double(get(handles.edit24,'string')) str2double(get(handles.edit25,'string')) str2double(get(handles.edit27,'string')) ]
case 4
m=[str2double(get(handles.edit3,'string')) str2double(get(handles.edit4,'string')) str2double(get(handles.edit6,'string')) str2double(get(handles.edit7,'string')) str2double(get(handles.edit8,'string')) (str2double(get(handles.edit9,'string'))); str2double(get(handles.edit10,'string')) str2double(get(handles.edit11,'string')) str2double(get(handles.edit12,'string')) str2double(get(handles.edit13,'string')) str2double(get(handles.edit14,'string')) (str2double(get(handles.edit15,'string'))); str2double(get(handles.edit16,'string')) str2double(get(handles.edit17,'string')) str2double(get(handles.edit18,'string')) str2double(get(handles.edit19,'string')) str2double(get(handles.edit20,'string')) (str2double(get(handles.edit21,'string'))); str2double(get(handles.edit22,'string')) str2double(get(handles.edit23,'string')) str2double(get(handles.edit24,'string')) str2double(get(handles.edit25,'string')) str2double(get(handles.edit26,'string')) (str2double(get(handles.edit27,'string'))); str2double(get(handles.edit28,'string')) str2double(get(handles.edit29,'string')) str2double(get(handles.edit30,'string')) str2double(get(handles.edit31,'string')) str2double(get(handles.edit32,'string')) str2double(get(handles.edit33,'string')) ]
otherwise
x=0
end
  1 comentario
Stephen23
Stephen23 el 3 de Nov. de 2017
Editada: Stephen23 el 3 de Nov. de 2017
All of those individually numbered fieldnames make your code much more complex than it needs to be. Just use an array and indexing to make your code much simpler, more versatile, and less buggy.
Any time that you find yourself copy-and-pasting code with just minor changes then you really need to reconsider your code design.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 2 de Nov. de 2017
Editada: Walter Roberson el 2 de Nov. de 2017
There are some ways to prevent a matrix from zero filling as it is extended:
  1. Do not extend it. Assign it full size at the beginning, and put in some non-zero value for the unused space. For example initialize as nan(3,4)
  2. If you must extend it, then at each point where it might be extended, check if you are extending it, and carefully assign some non-zero value for the extension areas that are not to be zero filled.
  3. Instead of using a numeric array, use a cell array initialized to empty arrays until it is completely filled out. You might need to write code to display intermediate results.
  2 comentarios
Erwin Avendaño
Erwin Avendaño el 2 de Nov. de 2017
I would like to do option 3 that you ask me, but the truth is that I do not know how to start programming in matlab, I am programming but with trial and error with what I see on YouTube and I read on the Internet. My option so that it did not mark error was that at the beginning when pressing the user 2x2, 3x3, 4x4 or 5x5 that when pressing that button it will generate a number that is for the button 2x2 u = 1, for the button 3x3 u = 2 and thus, when the user presses the button to solve equations in the code, the first thing you will see will be the value of u, with the change statement if u = 1, then it will be completed with a 2x3 if u = 2 will be filled for a 3x4 and, therefore, there would not be an extended matrix but I do not know how to do that when I press the 2x2 button that generates u = 1 and save it so that later when I press the button to solve equations check the value of u and thus have no error . annex screenshot of matlab gui

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by