What is the best way to store different variables into 1 variable?

I need to take a bunch of coordinate points that I have stored in a variable, and sort them in groups of the ten closest endpoint triplet groups to each other triplet. Then, I need to store them along with another group of coordinate points that correspond to the central points of each of these triplets. What's the best way to do this? The attached file contains all the (x,y) coordinates for the endpoint triplets (first 6 columns), and the central points (last two columns) Thanks in advance for your help!

Answers (1)

Use a cell array. Or use a srruct. Either can store anything you put into it.
Here, a struct will allow you to store everything with a name attached to each field, so it will be arguably a better choice. And you can have arrays of structs.

5 Comments

Thanks so much John. I will definitely use that next. For now, I need to be able to get this code to work, in that I need to store 10 groups of three points which are closest to a source group. Please see below:
close all;
clearvars;
% load('F_points.mat');
load('fpep.mat');
n = 10;
for pt = 1 : 953
dist = sqrt((fpep(:,7)-fpep(pt,7)).^2 + (fpep(:,8)-fpep(pt,8)).^2);
[~, ascendIdx] = sort(dist);
xyNearest(pt,:,:) = fpep(ascendIdx(1:n),7:8);
adirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),1) - xyNearest(pt,:,1)]);
adirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),2) - xyNearest(pt,:,2)]);
bdirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),3) - xyNearest(pt,:,1)]);
bdirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),4) - xyNearest(pt,:,2)]);
cdirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),5) - xyNearest(pt,:,1)]);
cdirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),6) - xyNearest(pt,:,2)]);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
end
chordx = ([xyNearest(:,:,1) - fpep(:,7)]);
chordy = ([xyNearest(:,:,2) - fpep(:,8)]);
chord = sqrt((chordx).^2 + (chordy).^2);
if pt>0
Under what circumstances could pt be <= 0 ?
That is just a temporary perpetual "if" place-holder Walter.
You overwrite ascendIdx each iteration of the loop, so it is not clear why you would bother to do
ascendIdx(ascendIdx==pt) = []; %remove the pt point
It would make more sense if that was just after the sort()
Could you give me an example of how you would set up the proper code?

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 15 Oct 2019

Commented:

on 17 Oct 2019

Community Treasure Hunt

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

Start Hunting!