What is the correct syntax to avoid 'Error: ()-indexing must appear last in an index expression'
1 view (last 30 days)
Show older comments
Hello, trying to make this code more interactive and efficient:
%Ask user Flight Condition
FlightCond=input('Which flight condition?')
for j = 1:numel(manoev)
DataSetU(num2str(FlightCond))=DataBase((numel(FlightCond)),j);
%<-problem here?
end
%Ask user which manoevre
time=(DatasetU(num2str(manoev))(:,1));
q=DatasetU(num2str(manoev))(:,4);
thanks
Note: DataBase is a cell array 3x5 manoev=[1 2 3 4 5]
0 Comments
Accepted Answer
dpb
on 2 Dec 2014
_DataSetU(num2str(FlightCond))=DataBase((numel(FlightCond)),j);_
To get the value within a cell use the curlies...
DataBase{numel(FlightCond),j}
This looks peculiar, however as numel(FlightCond) will be 1 always one would presume as the requested value given by the user. Don't you really just want that value (which should be tested to be within the range 1:3 before being used)?
DataBase{FlightCond,j} % looks to me like what you want for RHS
_DataSetU(num2str(FlightCond))_ looks funky, too... *num2str* is going to turn the _FlightCond_ numeric variable into a string representation. When use that as an array index Matlab will convert it to values of the ASCII characters for the numeric representation and you'll get an array of something like 32 elements. This should just be
DataSetU(FlightCond) = ...
Again, you need to test inputs to be in range.
And, since you're moving the row and not changing the row, the loop can be rewritten as
DataSetU(FlightCond)=DataBase{FlightCond,:};
and dispense with the loop altogether.
And, for ease for your user (or you :) ), I'd suggest using a listdlg for the selection--that will ensure a valid selection or an escape/abort and eliminate typo's, etc., ...
2 Comments
dpb
on 3 Dec 2014
Let's see the actual revised code in context...you do not want num2str here. See what
q(num2str(3))=pi
does at the command line...
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!