sym and string deprecated

11 Ansichten (letzte 30 Tage)
David Ling
David Ling am 30 Nov. 2017
Kommentiert: Walter Roberson am 1 Dez. 2017
I got a warning like this and can anyone spot the error from my code?
Warning: Support of character vectors that are not valid variable names or define a number will be removed in a future release. To create
symbolic expressions, first create symbolic variables and then use operations on them.
> In sym>convertExpression (line 1559)
In sym>convertChar (line 1464)
In sym>tomupad (line 1216)
In sym (line 179)
In sym/subs (line 57)
My code is here:
[...
'set(guiele.LSIMWIND,''pointer'',''watch'');',...
'drawnow;',...
'clear t0,ts0=[];t0=str2num(char(get(guiele.h(1,2),''String'')));',...
'clear tstep,tstep=[];tstep=str2num(char(get(guiele.h(2,2),''String'')));',...
'clear tsim,tsim=[];tsim=str2num(char(get(guiele.h(3,2),''String'')));',...
'clear input,input=[];input=char(get(guiele.h(4,2),''String''));',...
'syms t;vabls.tset=[t0:tstep:tsim];vabls.numinput=subs(input,t,vabls.tset);',...
'figure(guiele.CONTWIND);',...
'if ishandle(guiele.ResponsePlotAxis) delete(guiele.ResponsePlotAxis);end;',...
'guiele.ResponsePlotAxis=axes(''parent'',guiele.ResponsePlotPanel,''tag'',''ResponsePlotAxis'',''units'',''normal'',''position'',[0.1267 0.1600 0.8183 0.7217]);hold on;',... %this gets deleted in the Calculate button callback
'[vabls.ResponseYdata vabls.ResponseXdata] = lsim(vabls.ctf,double(vabls.numinput),vabls.tset);',...
'guiele.ResponsePlotLine=plot(vabls.ResponseXdata,vabls.ResponseYdata);',...
'guiele.InputPlotLine=plot(vabls.tset,vabls.numinput,''r'');',...
'legend(''Response'',''Input'',''location'',''best'');',...
'xlabel('' Time (s)'');ylabel(''Amplitude'');',...
'eval(cnstn.ResponsePlotHousekeeping);',...
'close(guiele.LSIMWIND);',...
]
This is a string callback. I am not sure what is wrong in here.

Antworten (1)

Walter Roberson
Walter Roberson am 30 Nov. 2017
You have
'clear input,input=[];input=char(get(guiele.h(4,2),''String''));',...
'syms t;vabls.tset=[t0:tstep:tsim];vabls.numinput=subs(input,t,vabls.tset);',...
This gets a character vector from guiele.h(4,2) and stores it in the variable input, and then passes that variable input as the first parameter to the symbolic routine subs . When subs() is passed a character vector as its first parameter, it converts it to symbolic by using sym() in the character vector.
When sym() is applied to a character vector, it mostly treats the character vector as being input in the MuPAD programming language, but it does do some preprocessing to convert some MATLAB constructs to MuPAD, such as converting '[' ']' arrays into the internal MuPAD form, and converting space-separated elements to comma separated.
Mathworks is trying to get away from users writing anything in MuPAD language form, so they are having sym() send out warning messages when it is asked to convert a string that contains anything other than pure numbers or pure variable names. These are not error messages (...yet...). They have been giving this warning for several releases, but it is not until R2017b that they implemented a replacement routine.
The R2017b new str2sym() routine takes MATLAB language strings, not MuPAD language strings, and does the appropriate conversions.
... Really you should be recoding that entire callback in terms of calling an anonymous function to do the work instead of using a big quoted string like that.
  4 Kommentare
David Ling
David Ling am 1 Dez. 2017
I am using R2016b so I should just turn off the warning?
Walter Roberson
Walter Roberson am 1 Dez. 2017
If the user is expected to input MuPAD language strings, then you should leave the warning on as a reminder that it was not a good idea to expect users to input MuPAD language strings.
If the user is expected to input MATLAB language strings, you should probably be converting to function handles with fixed variable names. Perhaps
F=str2func(['@(t)' input]); vabls.numinput=F(vabls.tset);

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by