Indexing can't yield multiple results.

2 views (last 30 days)
I am trying to save multiple values from a database as variables at once by indexing. I don't know why this isn't working but might have the wrong method, if this doesn't work how do I do this:
[x, y, z, vx, vy, vz] = EarthPosData(Start,:);
the database gives me the correct result since:
>> EarthPosData(Start,:)
ans =
1.0e+08 *
1.4810 -0.2964 -0.0002 0.0000 0.0000 -0.0000
So it seems that I just can't use this method? so then how do I get xyz... without having 6 lines of code?
thanks

Accepted Answer

Star Strider
Star Strider on 12 Sep 2016
You can do something similar to that with a cell array or the deal function, but the right side of the assignment has to be a comma-separated list.
This works:
EarthPosData = randi(9, 3, 6); % Create Data
EarthPosDataCell = mat2cell(EarthPosData, size(EarthPosData,1), ones(1,size(EarthPosData,2)));
[x, y, z, vx, vy, vz] = EarthPosDataCell{:};
You’ll have to verify that it works with your data.
  5 Comments
Stephen23
Stephen23 on 12 Sep 2016
Edited: Stephen23 on 12 Sep 2016
@Martin Brandel: you will have to use a comma separated list. Star Strider's answer gives a reasonable way of doing this. Generally though, it is more convenient to keep data together, rather than splitting it apart: unless there is a great need to define those variables, personally I would just keep them in one array and using indexing or fieldnames to access them.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!