Error: indexing must appear last in an index expression.

1 vue (au cours des 30 derniers jours)
Elia
Elia le 27 Jan 2014
Commenté : Elia le 28 Jan 2014
i got the following error when i tried to run the code I wrote
Error using str2func
Error: ()-indexing must appear last in an index expression.
Error in Spro>VbR_button_Callback (line 836)
V=str2func(['@(t)
',char(diff(sym(regexprep(char(XFunction_current),'^@\(t\)',''))))]);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Spro (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)Spro('VbR_button_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Note: In my code the variable x is equal to the end value of the before last function I enter in .The new function I give or enter in the uitable can be depending on x ,but it should have just one input argument t , so i created x , so when I give x it will be as a number and not variable .
Tabledata=get(handles.Xtable,'data');
Positiondata=XTableCurrentdata(:,1); % matrix of Strings (functions)
Timedata=XTableCurrentdata(:,2); %Matrix of Time intervals
n=size(Tabledata,1);
for i=1:n
Tcurrent = str2num(Timedata{i,1}); %t2 end time of the current function [t1 t2]
EndTime_current=max(Tcurrent);
Xfunc_current= str2func(['@(t,x)' Positiondata{i,1}]);
X(1)=0;
X(i+1)=Xfunc_current(EndTime_current,X(i));
XFunction_current=@(t) Xfunc_current(t,X(i));
V=str2func(['@(t) ',char(diff(sym(regexprep(char(XFunction_current),'^@\
(t\)',''))))]);
...
  2 commentaires
Amit
Amit le 27 Jan 2014
I am sorry Sam. My answer was wrong and very wrong. Thats why I deleted it. Your code is right upto the first str2func. The issue is in second str2fun. I donot know much of symbolic toolbox but there will be many here who can help you.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Jan 2014
You would be better off using anonymous functions to capture the value of the variable instead of trying to convert the variable to a specific value in a string and re-converting the string to a function.
You should also consider using func2str() instead of the char() stuff you are using.
As you are using sym() you are using the symbolic toolbox. A better way to do diff() of a function handle F(x), rather than stripping off the anonymous header from char() of the function handle and diff()'ing sym() of that, is to use something like
sym t
diff(F(t),t)
You can then use matlabFunction to convert that symbolic expression back to a function handle.
  1 commentaire
Elia
Elia le 28 Jan 2014
it works thank you , but I used 'str2func(['@(t)' char(F)])' to convert the symbolic expression back to a function handle. because I got error with using matlabFunction when the the function is a polynomial with degree 1 or a constant.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by