Indexing can't yield multiple results.
2 views (last 30 days)
Show older comments
Martin Brandel
on 12 Sep 2016
Commented: Martin Brandel
on 12 Sep 2016
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
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!