How to Solve Equations with Missing Parameters in MATLAB app designer

5 visualizaciones (últimos 30 días)
The situation is like this: (x*y*z)=(p*q*r) is the equation.
M = (x*y*z)=(p*q*r) so, x and p is need to be filled out by users (compulsory) . At least 3 out of 4 parameters which are y,z, q,r need to be filled out. The pov from each parameters are as followed. y = (p*q*r)/(z*x) , z = (p*q*r)/(x*y), q= (x*y*z)/(p*r), r= (x*y*z)/(p*q) . how to have output for the parameter that we haven't filled out as well as the M. if the user input four of the parameters then the app should calculate the M only. if the users input 3 out of 4 of the paramters input, app should calculate the parameter that doesn't input as well as the M. Anyway the p and x is always need to be input by the user. Is there a way to fix this problem where the calculated value appear on the edit value itself for the y,z,q and r. Please help.
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
% Retrieve input values
x = app.xEditField.Value;
p = app.pEditField.Value;
y = app.yEditField.Value;
z = app.zEditField.Value;
q = app.qEditField.Value;
r = app.rEditField.Value;
% Check compulsory inputs are provided
if isempty(x) || isempty(p)
% error message
msgbox('Compulsory inputs must be provided.', 'Error', 'error');
return;
end
% Check the number of provided parameters
providedParameters = [~isempty(y), ~isempty(z), ~isempty(q), ~isempty(r)];
missingIndex = find(~providedParameters);
if missingIndex == 1
app.yEditField = (p*q*r)/(z*x) ;
elseif missingIndex == 2
app.zEditField= (p*q*r)/(x*y);
elseif missingIndex == 3
app.qEditField = (x*y*z)/(p*r);
elseif missingIndex == 4
app.rEditField = (x*y*z)/(p*q);
end
M = x*y*z; % since M = p*q*r = x*y*z
app.MEditField.Value = round(M, 2);
app.MEditField.Editable = false;
end

Respuestas (2)

Cris LaPierre
Cris LaPierre el 27 de Nov. de 2023
You probably need to have a check that no more than one of the 6 values is missing.
Also, you need to assign the calculated results to the correct property of your EditField (Value). You also need to update the corresponding variable value in your callback function.
y = (p*q*r)/(z*x);
app.yEditField.Value = y;

Aiswarya
Aiswarya el 28 de Nov. de 2023
Editada: Aiswarya el 28 de Nov. de 2023
Hi,
I understand that you are trying to figure out how to use edit field parameters which may have missing values in equations in MATLAB App Designer.
From your code, it seems you are using "Edit Field (Numeric)" in your app and using "isempty()" in your code to detect if the values are missing. This doesn't work as all the numeric fields have a default value of zero. Starting from R2023b, MATLAB allows adding empty edit numeric field by using the 'AllowEmpty' property (https://www.mathworks.com/help/matlab/ref/matlab.ui.control.numericeditfield-properties.html#mw_0f3e4504-cab5-480d-a88e-16adacfcd499)
as shown below :
You should tick the "AllowEmpty" property and change the "Value" property from 0 to blank. The corresponding lines in Code View for the same are (say for parameter r):
app.rEditField.AllowEmpty = 'on';
app.rEditField.Value = [];
This step has to be done for all the 6 parameters. After doing this, please refer to the below code which includes some minor corrections to your code:
% Check the number of provided parameters
providedParameters = [isempty(y), isempty(z), isempty(q), isempty(r)];
% To give error if less than 3 out of 4 parameters are provided
if sum(providedParameters) > 1
msgbox('At least 3 out of 4 parameters must be provided.', 'Error', 'error');
return;
end
if isempty(y)
app.yEditField.Value = (p*q*r)/(z*x) ;
elseif isempty(z)
app.zEditField.Value = (p*q*r)/(x*y);
elseif isempty(q)
app.qEditField.Value = (x*y*z)/(p*r);
elseif isempty(r)
app.rEditField.Value = (x*y*z)/(p*q);
end
M = app.xEditField.Value * app.yEditField.Value * app.zEditField.Value; % since M = p*q*r = x*y*z
app.MEditField.Value = round(M, 2);
app.MEditField.Editable = false;
In case you are using an older version of MATLAB, you may use "Edit Field (Text)" instead and use "isnan" to find the empty fields. You would need to use functions to convert string to numeric and vice versa.
Convert all 6 parameters to double:
x = str2double(app.xEditField.Value); % Do this step for all parameters
A workaround code to do the same in an older version of MATLAB is given below:
% Check the number of provided parameters
if isnan(x) || isnan(p)
% Display an error message or handle the case where x and p are not provided
msgbox('Compulsory inputs must be provided.', 'Error', 'error');
return;
else
providedParameters = [isnan(y), isnan(z), isnan(q), isnan(r)];
if sum(providedParameters) > 1
msgbox('At least 3 out of 4 parameters must be provided.', 'Error', 'error');
return;
end
% Calculate missing parameters if 3 out of 4 are provided
if isnan(y)
y = (p * q * r) / (x * z);
app.yEditField.Value = num2str(y);
elseif isnan(z)
z = (p * q * r) / (x * y);
app.zEditField.Value = num2str(z);
elseif isnan(q)
q = (x * y * z) / (p * r);
app.qEditField.Value = num2str(q);
elseif isnan(r)
r = (x * y * z) / (p * q);
app.rEditField.Value = num2str(r);
end
%Calculate M
M = x * y * z;
app.MEditField.Value = num2str(round(M, 2));
app.MEditField.Editable = false;
end

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by