Error: indexing must appear last in an index expression.

1 回表示 (過去 30 日間)
Elia
Elia 2014 年 1 月 27 日
コメント済み: Elia 2014 年 1 月 28 日
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 件のコメント
Amit
Amit 2014 年 1 月 27 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2014 年 1 月 27 日
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 件のコメント
Elia
Elia 2014 年 1 月 28 日
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.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by