Implementation .m file into Appdesigner troubleshot

1 次查看(过去 30 天)
I try to create diffusion simple calculator with app designer I have everything done and pre-code in .m files, now I try to implement that into appdesigner where I change the input (like wants appdesigner) and output(like want appdesigner) but in output I have some problem because I can´t RUN it normally can anyone help me? Or explain me what happening? I do this only for my study and I need the pass throught this...
and this is a code (with pre-maid appdesigner code)
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
REditField matlab.ui.control.NumericEditField
REditFieldLabel matlab.ui.control.Label
ubytokkoncentraciejeEditField matlab.ui.control.NumericEditField
ubytokkoncentraciejeEditFieldLabel matlab.ui.control.Label
EditField matlab.ui.control.NumericEditField
EditFieldLabel matlab.ui.control.Label
VypotajButton matlab.ui.control.StateButton
vstupdtEditField matlab.ui.control.NumericEditField
vstupdtEditFieldLabel matlab.ui.control.Label
vstuptEditField matlab.ui.control.NumericEditField
vstuptEditFieldLabel matlab.ui.control.Label
vstupTEditField matlab.ui.control.NumericEditField
vstupTEditFieldLabel matlab.ui.control.Label
vstupk0EditField matlab.ui.control.NumericEditField
vstupk0EditFieldLabel matlab.ui.control.Label
vstupEEditField matlab.ui.control.NumericEditField
vstupEEditFieldLabel matlab.ui.control.Label
zadajvstupC0EditField matlab.ui.control.NumericEditField
zadajvstupC0EditFieldLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: zadajvstupC0EditField
function zadajvstupC0EditFieldValueChanged(app, event)
end
% Value changed function: VypotajButton
function VypotajButtonValueChanged(app, event)
in = data(); % načitanie vstupu
for k = 1:length(in) %vypis vstupu
in(k)
end
[tau1, c]=C(in);
[tau2,x]=X(in);
tau2 = tau2';
x = x';
table1 = [tau1 c];
writematrix(table1,"vystup1.xls");
table2 = [tau2 x];
writematrix(table2,"vystup2.xls");
%print1Var = num2str(c(length(c)));
%print2var = num2str(x(length(x)));
app.EditField.Value = double(num2str(c(length(c))));
app.ubytokkoncentraciejeEditField.Value = double(num2str(x(length(x))));
function in = data(in)
in.c0 = app.zadajvstupC0EditField.Value;
in.E = app.vstupEEditField.Value;
in.k0 = app.vstupk0EditField.Value;
in.T = app.vstupTEditField.Value;
in.t = app.vstuptEditField.Value;
in.dt = app.vstupdtEditField.Value;
in.R=8.314;
end
%c.m
function [tau, c]= C(in)
options = odeset;
%vypočet diff. rovnice
[tau,c]=ode23(@dif_rovnica,[0 in.t],in.c0,options,in);
end
function dcdt = dif_rovnica(tau,c,in)
dcdt = zeros(1,1);
k = in.k0*exp(-(in.E/(in.R*in.T)));
dcdt = -k*c;
end
%x.m
function[tau,x]= X(in)
i=1;
cas = 0;
while(cas<=in.t)
tau(i)=cas;
k=in.k0*exp(-(in.E/(in.R*in.T)));
x(i)=in.c0*(1-exp(-k*cas));
cas = cas+in.dt;
i=i+1;
end
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 764 594];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [293 225 415 303];
% Create zadajvstupC0EditFieldLabel
app.zadajvstupC0EditFieldLabel = uilabel(app.UIFigure);
app.zadajvstupC0EditFieldLabel.HorizontalAlignment = 'right';
app.zadajvstupC0EditFieldLabel.Position = [54 556 91 22];
app.zadajvstupC0EditFieldLabel.Text = 'zadaj vstup C0 :';
% Create zadajvstupC0EditField
app.zadajvstupC0EditField = uieditfield(app.UIFigure, 'numeric');
app.zadajvstupC0EditField.ValueChangedFcn = createCallbackFcn(app, @zadajvstupC0EditFieldValueChanged, true);
app.zadajvstupC0EditField.Position = [160 556 100 22];
% Create vstupEEditFieldLabel
app.vstupEEditFieldLabel = uilabel(app.UIFigure);
app.vstupEEditFieldLabel.HorizontalAlignment = 'right';
app.vstupEEditFieldLabel.Position = [97 519 55 22];
app.vstupEEditFieldLabel.Text = 'vstup E : ';
% Create vstupEEditField
app.vstupEEditField = uieditfield(app.UIFigure, 'numeric');
app.vstupEEditField.Position = [160 515 100 22];
% Create vstupk0EditFieldLabel
app.vstupk0EditFieldLabel = uilabel(app.UIFigure);
app.vstupk0EditFieldLabel.HorizontalAlignment = 'right';
app.vstupk0EditFieldLabel.Position = [93 482 60 22];
app.vstupk0EditFieldLabel.Text = 'vstup k0 : ';
% Create vstupk0EditField
app.vstupk0EditField = uieditfield(app.UIFigure, 'numeric');
app.vstupk0EditField.Position = [160 474 100 22];
% Create vstupTEditFieldLabel
app.vstupTEditFieldLabel = uilabel(app.UIFigure);
app.vstupTEditFieldLabel.HorizontalAlignment = 'right';
app.vstupTEditFieldLabel.Position = [99 443 54 22];
app.vstupTEditFieldLabel.Text = 'vstup T : ';
% Create vstupTEditField
app.vstupTEditField = uieditfield(app.UIFigure, 'numeric');
app.vstupTEditField.Position = [160 433 100 22];
% Create vstuptEditFieldLabel
app.vstuptEditFieldLabel = uilabel(app.UIFigure);
app.vstuptEditFieldLabel.HorizontalAlignment = 'right';
app.vstuptEditFieldLabel.Position = [103 397 50 22];
app.vstuptEditFieldLabel.Text = 'vstup t : ';
% Create vstuptEditField
app.vstuptEditField = uieditfield(app.UIFigure, 'numeric');
app.vstuptEditField.Position = [160 392 100 22];
% Create vstupdtEditFieldLabel
app.vstupdtEditFieldLabel = uilabel(app.UIFigure);
app.vstupdtEditFieldLabel.HorizontalAlignment = 'right';
app.vstupdtEditFieldLabel.Position = [96 352 57 22];
app.vstupdtEditFieldLabel.Text = 'vstup dt : ';
% Create vstupdtEditField
app.vstupdtEditField = uieditfield(app.UIFigure, 'numeric');
app.vstupdtEditField.Position = [160 352 100 22];
% Create VypotajButton
app.VypotajButton = uibutton(app.UIFigure, 'state');
app.VypotajButton.ValueChangedFcn = createCallbackFcn(app, @VypotajButtonValueChanged, true);
app.VypotajButton.Text = 'Vypočítaj';
app.VypotajButton.Position = [160 131 100 23];
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.Position = [1 90 533 30];
app.EditFieldLabel.Text = {'Vypočet zmeny koncentracie C a ubytku koncentracie x reaktantou pri priebehu chemicej rekacie '; ' "vysledok koncentracie je"'};
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'numeric');
app.EditField.Position = [541 98 88 22];
% Create ubytokkoncentraciejeEditFieldLabel
app.ubytokkoncentraciejeEditFieldLabel = uilabel(app.UIFigure);
app.ubytokkoncentraciejeEditFieldLabel.HorizontalAlignment = 'right';
app.ubytokkoncentraciejeEditFieldLabel.Position = [403 48 136 22];
app.ubytokkoncentraciejeEditFieldLabel.Text = '"ubytok koncentracie je "';
% Create ubytokkoncentraciejeEditField
app.ubytokkoncentraciejeEditField = uieditfield(app.UIFigure, 'numeric');
app.ubytokkoncentraciejeEditField.Position = [546 48 88 22];
% Create REditFieldLabel
app.REditFieldLabel = uilabel(app.UIFigure);
app.REditFieldLabel.HorizontalAlignment = 'right';
app.REditFieldLabel.Position = [609 556 25 22];
app.REditFieldLabel.Text = 'R';
% Create REditField
app.REditField = uieditfield(app.UIFigure, 'numeric');
app.REditField.Position = [649 556 100 22];
app.REditField.Value = 8.314;
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
I attache the .m files what I try to implement into appdesigner app:
  7 个评论
martin Kostovcik
martin Kostovcik 2022-4-29
编辑:martin Kostovcik 2022-4-29
I initialize that variables like "10" and then I try to calculate that with all thees variables into VypotajButtonValueChanged (after initialize when all vars are == 10 )
I use thees variables for calculations C0,E,k0,T,t,dt and R where C0,E,k0,T,t,dt its set == "10" and R = 8.314
like you can see here:
or did I misunderstand something?
martin Kostovcik
martin Kostovcik 2022-4-29
And expected output ("-1.1733e-06") is for the first edit field where is "0"
and the second edit field where is "0" its expected output "10"

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2022-4-29
Which of those lines, line 55 or 56, throws the error?
Set a breakpoint on that line. Then depending on which line throws the error show us the contents of the variable c or x. According to the description of the Value property for NumericEditField components on the documentation page:
"MATLAB rejects the value if:
  • It cannot convert the character vector to a scalar number.
  • The value is NaN, blank, or a complex number.
  • The value is a mathematical expression, such as 1 + 2.
  • The value is less than the Limits property lower limit or greater than the upper limit."
Ah, I see the problem. You don't want to use num2str here.
n = num2str(123)
n = '123'
d = double(n)
d = 1×3
49 50 51
The variable d contains the ASCII values for the characters in n and is not scalar even though 123 is because 123 has more than 1 digit. Just directly set the Value property to either c(end) or x(end).
q = 123
q = 123
isscalar(n) % False
ans = logical
0
isscalar(d) % False
ans = logical
0
isscalar(q) % True
ans = logical
1
  3 个评论
Steven Lord
Steven Lord 2022-4-29
You don't want that field to contain 0 (if the value is not scalar isscalar will return false which is displayed as 0) or 1 (if isscalar returns true.) You want it to display the actual value. Get rid of the isscalar call and the num2str call.
app.EditField.Value = c(end);
app.ubytokkoncentraciejeEditField.Value = x(end);
martin Kostovcik
martin Kostovcik 2022-4-29
Yeeeh, there was the problem
now its works well. thanks guys

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by