Help with Z transform using app designer
16 views (last 30 days)
Show older comments
Hello !!
Sorry if there is already a solution to this but i couldnt find it. I am still new to matlab and app design.
so what im trying to do here is to create an app that can calculate z transform of a sequence that the user will input. e.g [1 2 3 4] with intial n and final n. here is the code
function ZtransformButtonPushed(app, event)
x = app.InputsequenceofsignalxnTextArea.Value;
n1 = app.InitialvalueofnEditField.Value;
n2 = app.FinalvalueofnEditField.Value;
syms z y m;
f(y,m)=(y*(z^(-m)));
k=1;
for n=n1:1:n2
answer(k)=f((x(k)),n);
k=k+1;
end
app.ZtransformoftheinputsequenceTextArea.Value = sum(answer);
end
this is the error:
Error using sym>convertChar (line 1448)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.
Error in sym>tomupad (line 1214)
S = convertChar(x);
Error in sym (line 211)
S.s = tomupad(x);
Error in cell2sym (line 31)
Csym = cellfun(conv, C, 'UniformOutput', false);
Error in sym>tomupad (line 1226)
xsym = cell2sym(x);
Error in sym (line 211)
S.s = tomupad(x);
Error in sym/subs>@(x)sym(x) (line 209)
Y = cellfun(@(x)sym(x),Y,'UniformOutput',false);
Error in sym/subs>normalize (line 209)
Y = cellfun(@(x)sym(x),Y,'UniformOutput',false);
Error in sym/subs>mupadsubs (line 157)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
Error in symfun/feval>evalScalarFun (line 42)
y = subs(formula(Ffun), Fvars, inds);
Error in symfun/feval (line 28)
varargout{1} = evalScalarFun(F, Fvars, varargin);
Error in symfun/subsref (line 178)
B = feval(A,inds{:});
2 Comments
Mario Malic
on 4 Oct 2020
Edited: Mario Malic
on 4 Oct 2020
Thanks for asking the question properly. For the cleaniness of this question (and future ones), you can format the code properly by using the button just above the text field, or marking your code and pressing Alt + Enter.
Your field contains a text value, which is in your case character array, you can use str2num to get a numeric array of input.
x = str2num(app.InputsequenceofsignalxnTextArea.Value);
Similar issue can be seen in one of the last line, where you assign a numeric value to character to text field
app.ZtransformoftheinputsequenceTextArea.Value = sum(answer);
If this field is supposed to only display numeric values, it's better to change to the component "Edit field (numeric value)", otherwise, you can use
app.ZtransformoftheinputsequenceTextArea.Value = num2str(sum(answer));
I am unable to comment if there are any issues on symbollic expressions, as I have not worked with them yet.
Answers (1)
Walter Roberson
on 4 Oct 2020
The Value of a text area is a string() array. You are passing the first element of the string array as x(k) into the symbolic function f.
It is legal sometimes to pass a single string as a parameter to a symbolic function, but only under the circumstances that the string is a pure (unindexed) variable name, or is a representation of a pure numeric constant.
For numeric constants the only operator permitted is unary plus or unary minus, and + or - between the parts of a complex constant.
For variable names, no operators are permitted.
If you are expecting that the user is entering a formula that is to be transformed, then you cannot do that directly like you are, and would instead need to str2sym(x(i))
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!