How to find the index from a large scatter plot in matlab? - matlab

I have a scatter plot with about 6000 items.
x = rand(1,6000);
y = rand(1,6000);
scatter(x,y)
Is there a way to find the index of a given point using the GUI? (We zoom into data, and want to find the specific index that gave rise to a point)

Here's a very simple solution:
Open a plot > data cursor > edit text update function
Set the text update function to:
function output_txt = myFunction(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
% Import x and y
x = get(get(event_obj,'Target'),'XData');
y = get(get(event_obj,'Target'),'YData');
% Find index
index_x = find(x == pos(1));
index_y = find(y == pos(2));
index = intersect(index_x,index_y);
% Set output text
output_txt = {['X: ',num2str(pos(1),4)], ...
['Y: ',num2str(pos(2),4)], ...
['Index: ', num2str(index)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end
Results:
Just FYI, if you want to do this programatically, then here's a good article on it here.

You can use the X, Y position to search for the point:
%export the cursor to the workspace
possibleXpositions = find(x == cursor_info.Position(1));
possibleYPositions = find(y == cursor_info.Position(2));
position = intersect(possibleXpositions, possibleYPositions);
position will hold the index of your selected random number.
As a one liner:
position = intersect(find(x == cursor_info.Position(1)), find(y == cursor_info.Position(2)));

Related

How to cut part of the data out of a plot in Matlab

I just wanted to cut part of my data out in MATLAB, for example:
If I click on two points on the axis, it will cut the elements after the I click on with respect to the x-axis. I will post my code and a pic for further details
Thank you in advance
load sample.mat
X = sample.current;
X1 = sample.voltage;
Ts = 0.01;
Fs = 1/Ts;
Fm = Fs/2;
Fc = 2;
N =10;
d = fdesign.lowpass('N,Fc',N,Fc,Fs);
designmethods(d);
Hd = design(d);
%fvtool(Hd)
%X is a variable form csv
%X1 is a variable from csv
output = filter(Hd,X);
output1 = filter(Hd,X1);
figure;
plot(X,X1,'-g');
hold on
plot(output, output1,'r');
hold off
legend('raw signal','filtered signal')
grid on
x = output, output1;
y = output1;
figure
subplot(2,1,1)
plot(x,y,'r');
title('Original plot');
uiwait(msgbox('Select an x-value from which to crop','modal'));
[x_user ~] = ginput(1); % Let the user select an x-value from which to crop.
x(x>x_user) = [];
subplot(2,1,2);
plot(x,y,'r');
title('New plot with cropped values');
xlim([min(x(:)) max(x(:))]);
enter image description here
*Posting this as an answer to format code.
If its only one graphic you can just select the points that you want to delete using the "Brush/Select Data" (icon of a brush with a red square located at the menubar of the figure) selecting the data you want to be gone and then pressing the delete key.
If you want to do it with code you can try to find the index of the point where the signal starts to decrease over the X using something like:
% Find the index where X starts to decrease
maxIndex = find(data.x == max(data.x));
% In case of multiple indexs, ensure we get the first one
maxIndex = maxIndex(1);
% Copy data to new vector
saveData.x = data.x(1:maxIndex);
saveData.y = data.y(1:maxIndex);
If you want to use the users' click position you can use find to locate the index of the first element after the click:
% Get the coords of the first click
userFirstClick = ginput(1);
% Get the X component of the coords
xCoordInit = userFirstClick(1);
% Locate the index of the first element that is greater than
% the xCoord
firstXIndex = find(data.x >= xCoordInit);
% In case of multiple indexs, ensure we get the first one
firstXIndex = firstXIndex(1);
% Do the same to get the final index
userSecondClick = ginput(1);
xCoordFinal = userSecondClick(1);
finalXIndex = find(data.x > xCoordFinal);
finalXIndex = finalXIndex(1)-1;
% -1 because data.x(finalXIndex) is already greater than xCoordFinal
% Copy data to the new vector
saveData.x = data.x(firstXIndex:finalXIndex);
saveData.y = data.y(firstXIndex:finalXIndex);
Then just plot saveData.
Edit
There was a typo on my previous code, here you have a fully functional example where you just need to click over the two points where you want to crop.
function cropSine()
% create a period of a Sine to initialize our data
data.x = -pi*3:0.01:pi*3;
data.y = sin(data.x);
% we make it loop back just as in your picture
data.x = [data.x,data.x(end:-1:1)];
data.y = [data.y, -data.y*0.5+5];
% create a figure to show the signal we have just created
figure
% create the axes where the data will be displayed
mainAx = axes();
% Draw our fancy sine!
plot(data.x, data.y, 'b-', 'Parent', mainAx);
% Request the initial position to crop
userFirstClick = ginput(1);
% Get the index of the nearest point
initIndex = getNearest(userFirstClick, data);
% Do the same to get the final index
userSecondClick = ginput(1);
% Get the index of the nearest point
finalIndex = getNearest(userSecondClick, data);
% check if its a valid point
if isempty(initIndex) || isempty(finalIndex)
disp('No points in data vector!');
return;
end
% Ensure that final index is greater than first index
if initIndex > finalIndex
tempVal = initIndex;
initIndex = finalIndex;
finalIndex = tempVal;
end
% Copy the data that we want to save into a new variable
saveData.x = data.x(initIndex:finalIndex);
saveData.y = data.y(initIndex:finalIndex);
% Plot the cropped data in red!
hold(mainAx, 'on');
plot(saveData.x, saveData.y, 'r-', 'Parent', mainAx);
hold(mainAx, 'off');
end
function nearestIndex = getNearest(clickPos, vector)
nearestIndex = [];
numPoints = length(vector.x);
if numPoints == 0
return;
end
nearestIndex = 1;
minDist = calcDist(vector.x(1), vector.y(1), clickPos(1), clickPos(2));
for pointID = 1:numPoints
dist = calcDist(vector.x(pointID), vector.y(pointID), clickPos(1), clickPos(2));
if dist < minDist
nearestIndex = pointID;
minDist = dist;
end
end
end
function dist = calcDist(p1x, p1y, p2x, p2y)
dist = sqrt(power(p1x-p2x,2)+power(p1y-p2y,2));
end

Extracting boundaries from an image manually and preventing overlap of points selected

I am trying to import the image boundaries into CAD modeling software. For doing that I am manually selecting the boundaries from this image. The code I am using is the following:
I = imread('image.jpg'); %image file name
imshow(I);
uiwait(msgbox('Left click to choose points. Right click to exit'));
n = 0;
while true
[x, y, button] = ginput(1);hold on;
if isempty(x) || button(1) ~= 1; break; end
n = n+1;
x_n(n) = x; % save all points you continue getting
y_n(n) = y;
plot(x,y,'-o',...
'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',10);
end
figure;
x_n=x_n';
y_n=y_n';
plot(x_n,y_n);
A=[x_n,y_n];
Selecting points on the boundaries as shown by this image give the following outcome in CAD software model. The overlap in the boundaries is because of the way lines are drawn in CAD software as shown below:
s1.Line(point1=(-0.131218,39.556604),point2=(1.762436,40.503431))
s1.Line(point1=(1.762436,40.503431),point2=(4.602916,38.136364))
s1.Line(point1=(4.602916,38.136364),point2=(9.337050,31.035163))
s1.Line(point1=(9.337050,31.035163),point2=(11.230703,23.460549))
s1.Line(point1=(11.230703,23.460549),point2=(14.544597,17.779588))
s1.Line(point1=(14.544597,17.779588),point2=(15.491424,12.572041))
s1.Line(point1=(15.491424,12.572041),point2=(16.438250,6.417667))
s1.Line(point1=(16.438250,6.417667),point2=(17.385077,2.156947))
s1.Line(point1=(17.385077,2.156947),point2=(21.645798,5.944254))
s1.Line(point1=(21.645798,5.944254),point2=(26.853345,8.784734))
s1.Line(point1=(26.853345,8.784734),point2=(31.114065,11.625214))
s1.Line(point1=(31.114065,11.625214),point2=(35.848199,13.045455))
s1.Line(point1=(35.848199,13.045455),point2=(40.582333,14.939108))
Is there a way where I can prevent the overlapping during selection by pausing the selection? Or any other suggestion on how to get the model made efficiently? I had tried using image processing to determine the boundaries. But it was beyond me to think something about converting the image coordinates into a CAD software model.
Possible solution is creating a polygon out of the selected points, and not adding selected point to x_n, y_n if there are lines crossings.
The code uses polyshape function for creating a polygon object:
clearvars
I = imread('image.jpg'); %image file name
imshow(I);
uiwait(msgbox('Left click to choose points. Right click to exit'));
warning('Off', 'MATLAB:polyshape:repairedBySimplify'); %Disable warning "Polyshape has duplicate vertices, intersections..."
n = 0;
h_pgon = [];
while true
[x, y, button] = ginput(1);hold on;
if isempty(x) || button(1) ~= 1; break; end
n = n+1;
%Save points to temporary arrays
tmp_x_n(n) = x; % save all points you continue getting
tmp_y_n(n) = y;
is_overlapping = false;
if n > 2
%Check if there is "overlapping during selection":
%Creates a polygon object
pgon = polyshape(tmp_x_n, tmp_y_n);
if (pgon.NumRegions > 1) || (pgon.NumHoles > 0)
%There are intersections, so don't add the new x, y to x_n, y_n.
disp('Overlapping!'); %Print "Overlapping!" for testing.
is_overlapping = true;
n = n - 1;
end
end
if ~is_overlapping
%There is no overlapping - copy tmp_x_n, tmp_y_n to x_n, y_n.
x_n = tmp_x_n;
y_n = tmp_y_n;
plot(x,y,'-o',...
'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',10);
%Plot polygon for testing
if n > 2
if isobject(h_pgon), delete(h_pgon);end %Delete previouse polygon.
h_pgon = plot(pgon);
end
end
end
figure;
x_n=x_n';
y_n=y_n';
plot(x_n,y_n);
A=[x_n,y_n];

MATLAB custom datatip in images

Following the instructions on other pages, e.g.
http://blogs.mathworks.com/videos/2011/10/19/tutorial-how-to-make-a-custom-data-tip-in-matlab/
http://it.mathworks.com/help/matlab/ref/datacursormode.html
http://it.mathworks.com/matlabcentral/answers/68079-how-to-add-additional-info-to-the-data-cursor
i've written a custom callback function for the datatip to show me the index of the points on a x-y graph as well as their x and y coordinates:
function output_txt = customCallback_DataTip(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
['Y: ',num2str(pos(2),4)]};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
else % 2D plot: write index of current point
i = find(event_obj.Target.XData == pos(1), 1);
output_txt{end+1} = ['i: ',num2str(i)];
end
This code starts from the default callback suggested by MATLAB, and adds a z-coordinate info whenever the plot is a 3D one. Since I very often need to know the array index of a point on a graph, the custom callback function is enabled automatically at MATLAB startup.
Now, whenever I plot an image (e.g. via imagesc) I would like to have the "normal" image datatip:
i.e. with Index/RGB information on it. How can I modify the callback function in order to obtain this behavior?
EDIT: i would like to modify my custom callback so that it automatically displays something similar to the default MATLAB default datatip when I'm using the datatip on an image.
To accomplish this, you can check the type of the event_obj.Target and respond accordingly.
get(event_obj.Target, 'type')
All images (whether imagesc, image, or imshow) will have a Type of image.
isImage = strcmpi(get(event_obj.Target, 'type'), 'image')
You can then extract the image data. If you have an indexed image, you can also get the colormap to determine all the other information to go into the datatip.
cdata = get(event_obj.Target, 'cdata');
cmap = colormap(ancestor(event_obj.Target, 'axes'));
Bringing this all together, I would modify your custom data tip callback to be something like this.
function output_txt = callback(obj, event_obj, clims)
% Get the cursor location
pos = get(event_obj, 'Position');
output_txt = {sprintf('[X,Y]: [%i, %i]', pos(1), pos(2))};
if strcmpi(get(event_obj.Target, 'type'), 'image')
% Get the image data
cdata = get(event_obj.Target, 'CData');
% Check to ensure pos is in range
if pos(1) < 1 || pos(1) > size(cdata, 2) || ...
pos(2) < 1 || pos(2) > size(cdata, 1)
rgb = {NaN, NaN, NaN};
newline = sprintf('[R,G,B]: [%0.4f %0.4f %0.4f]', rgb{:});
output_txt = cat(1, output_txt, newline);
return
end
% If the image is RGB
if size(cdata, 3) == 3
rgb = num2cell(cdata(pos(2), pos(1), :));
% If this is an indexed image
else
index = cdata(pos(2), pos(1));
% Figure out the colormap
hax = ancestor(event_obj.Target, 'axes');
cmap = colormap(hax);
% If the CData is scaled, we need to scale to the colormap
if strcmpi(get(event_obj.Target, 'CDataMapping'), 'scaled')
value = (index - clims(1)) * size(cmap, 1) / diff(clims);
else
value = index;
end
% Determine RGB value from colormap
rgb = num2cell(ind2rgb(round(value), cmap));
if round(index) == index
newline = sprintf('Index: %d', index);
else
newline = sprintf('Index: %.4f', index);
end
% Generate datatip text
output_txt = cat(1, output_txt, newline);
end
output_txt = cat(1, output_txt, ...
sprintf('[R,G,B]: [%0.4f %0.4f %0.4f]', rgb{:}));
% Otherwise we use your custom datatip for plots
else
index = find(event_obj.Target.XData == pos(1), 1);
pos = get(event_obj, 'Position');
output_txt = { sprintf('X: %0.4f', pos(1));
sprintf('Y: %0.4f', pos(2))};
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
output_txt{end+1} = sprintf('Z: %0.4f', pos(3));
else % 2D plot: write index of current point
output_txt{end+1} = sprintf('i: %d', index);
end
end
end
If you notice, I'm passing in an additional variable (clims) to the callback function. This is because some versions won't actually allow me to query axes properties from within the datatip UpdateFcn. So this means that you will have to change your UpdateFcn anonymous function just a little bit.
h = datacursormode(fig);
set(h, 'Enable', 'on')
% Here I have to pass the `clims` because I can't fetch them inside
set(h, 'UpdateFcn', #(dt,e)callback(dt, e, caxis(ancestor(dt.Host, 'axes'))));
Using this, I was able to show the proper display for both plots and images (both indexed and RGB).

Hough transform to detect and delete lines

I want to use the Hough transform to detect lines in my image.But instead of plotting the lines I want to delete each line detected in my original image.
image=imread('image.jpg');
image = im2bw(image);
BW=edge(image,'canny');
imshow(BW);
figure,imshow(BW);
[H,T,R] = hough(BW);
P = houghpeaks(H,100,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
Now after this I have got all the lines. But I want to delete all these lines from my original image, keeping rest of the image as before. Is there some way I can do this?
Edit I am uploading an image.I want to delete all the lines and keep the circular part.This is just an example image.Basically my objective is to delete the line segments and keep rest of the image
The issue you have is that your lines are thicker than one pixel.
The lines from the hough transform seem to be one pixel thick and
that doesn't help.
I propose that you delete the lines that you get from the Hough transform first.
This will sort of divide the hockey rink of whatever it is into segments
that will be easier to process.
Then you label each segment with bwlabel. For each object, find the
endpoints and fit a line between the endpoints. If the line and the object
have more pixels in common than a certain threshold, then we say that the object
is a line and we delete it from the image.
You may have to play around with the Hough transform's threshold value.
This technique has some flaws though. It will delete a filled square,
rectangle or circle but you haven't got any of those so you should be ok.
Results
Explanation
This is your code that I modified a bit. I removed the gradient because it
it easier to work with solid objects. The gradient gave very thin lines.
I also work on the complement image because the bw functions work with 1
as forgound rather than 0 as in your original image.
org_image_bw=im2bw(double(imread('http://i.stack.imgur.com/hcphc.png')));
image = imcomplement(org_image_bw);
[H,T,R] = hough(image);
P = houghpeaks(H,100,'threshold',ceil(0.27*max(H(:))));
lines = houghlines(image,T,R,P,'FillGap',5,'MinLength',7);
Loop through the lines you have got and delete them
processed_image = image;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
% // Use the question of a line y = kx + m to calulate x,y
% // Calculate the maximum number of elements in a line
numOfElems = max(max(xy(:,1))-min(xy(:,1)),max(xy(:,2))-min(xy(:,2)) ) ;
% // Cater for the special case where the equation of a line is
% // undefined, i.e. there is only one x value.
% // We use linspace rather than the colon operator because we want
% // x and y to have the same length and be evenly spaced.
if (diff(xy(:,1)) == 0)
y = round(linspace(min(xy(:,2)),max(xy(:,2)),numOfElems));
x = round(linspace(min(xy(:,1)),max(xy(:,1)),numOfElems));
else
k = diff(xy(:,2)) ./ diff(xy(:,1)); % // the slope
m = xy(1,2) - k.*xy(1,1); % // The crossing of the y-axis
x = round(linspace(min(xy(:,1)), max(xy(:,1)), numOfElems));
y = round(k.*x + m); % // the equation of a line
end
processed_image(y,x) = 0; % // delete the line
end
This is what the image looks after we have deleted the detected lines. Please note that the original hockey rink and been divided into multiple objects.
Label the remaining objects
L = bwlabel(processed_image);
Run through each object and find the end points.
Then fit a line to it. If, let's say 80% the fitted line covers
the object, then it is a line.
A fitted line could look like this. The diagonal blue line represents the fitted line and covers most of
the object (the white area). We therefore say that the object is a line.
% // Set the threshold
th = 0.8;
% // Loop through the objects
for objNr=1:max(L(:))
[objy, objx] = find(L==objNr);
% Find the end points
endpoints = [min(objx) min(objy) ...
;max(objx) max(objy)];
% Fit a line to it. y = kx + m
numOfElems = max(max(endpoints(:,1))-min(endpoints(:,1)),max(endpoints(:,2))-min(endpoints(:,2)) ) ;
% // Cater for the special case where the equation of a line is
% // undefined, i.e. there is only one x value
if (diff(endpoints(:,1)) == 0)
y = round(linspace(min(endpoints(:,2)),max(endpoints(:,2)),numOfElems));
x = round(linspace(min(endpoints(:,1)),max(endpoints(:,1)),numOfElems));
else
k = diff(endpoints(:,2)) ./ diff(endpoints(:,1)); % the slope
m = endpoints(1,2) - k.*endpoints(1,1); % The crossing of the y-axis
x = round(linspace(min(endpoints(:,1)), max(endpoints(:,1)), numOfElems));
y = round(k.*x + m);
% // Set any out of boundary items to the boundary
y(y>size(L,1)) = size(L,1);
end
% // Convert x and y to an index for easy comparison with the image
% // We sort them so that we are comparing the same pixels
fittedInd = sort(sub2ind(size(L),y,x)).';
objInd = sort(sub2ind(size(L),objy,objx));
% // Calculate the similarity. Intersect returns unique entities so we
% // use unique on fittedInd
fitrate = numel(intersect(fittedInd,objInd)) ./ numel(unique(fittedInd));
if (fitrate >= th)
L(objInd) = 0;
processed_image(objInd) = 0;
% // figure(1),imshow(processed_image)
end
end
Display the result
figure,imshow(image);title('Original');
figure,imshow(processed_image);title('Processed image');
Complete example
org_image_bw=im2bw(double(imread('http://i.stack.imgur.com/hcphc.png')));
image = imcomplement(org_image_bw);
[H,T,R] = hough(image);
P = houghpeaks(H,100,'threshold',ceil(0.27*max(H(:))));
lines = houghlines(image,T,R,P,'FillGap',5,'MinLength',7);
processed_image = image;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
% // Use the question of a line y = kx + m to calulate x,y
%Calculate the maximum number of elements in a line
numOfElems = max(max(xy(:,1))-min(xy(:,1)),max(xy(:,2))-min(xy(:,2)) ) ;
% // Cater for the special case where the equation of a line is
% // undefined, i.e. there is only one x value.
% // We use linspace rather than the colon operator because we want
% // x and y to have the same length and be evenly spaced.
if (diff(xy(:,1)) == 0)
y = round(linspace(min(xy(:,2)),max(xy(:,2)),numOfElems));
x = round(linspace(min(xy(:,1)),max(xy(:,1)),numOfElems));
else
k = diff(xy(:,2)) ./ diff(xy(:,1)); % the slope
m = xy(1,2) - k.*xy(1,1); % The crossing of the y-axis
x = round(linspace(min(xy(:,1)), max(xy(:,1)), numOfElems));
y = round(k.*x + m); % // the equation of a line
end
processed_image(y,x) = 0; % // delete the line
end
% // Label the remaining objects
L = bwlabel(processed_image);
% // Run through each object and find the end points.
% // Then fit a line to it. If, let's say 80% the fitted line covers
% // the object, then it is a line.
% // Set the threshold
th = 0.8;
% // Loop through the objects
for objNr=1:max(L(:))
[objy, objx] = find(L==objNr);
% Find the end points
endpoints = [min(objx) min(objy) ...
;max(objx) max(objy)];
% Fit a line to it. y = kx + m
numOfElems = max(max(endpoints(:,1))-min(endpoints(:,1)),max(endpoints(:,2))-min(endpoints(:,2)) ) ;
% Cater for the special case where the equation of a line is
% undefined, i.e. there is only one x value
if (diff(endpoints(:,1)) == 0)
y = round(linspace(min(endpoints(:,2)),max(endpoints(:,2)),numOfElems));
x = round(linspace(min(endpoints(:,1)),max(endpoints(:,1)),numOfElems));
else
k = diff(endpoints(:,2)) ./ diff(endpoints(:,1)); % the slope
m = endpoints(1,2) - k.*endpoints(1,1); % The crossing of the y-axis
x = round(linspace(min(endpoints(:,1)), max(endpoints(:,1)), numOfElems));
y = round(k.*x + m);
% // Set any out of boundary items to the boundary
y(y>size(L,1)) = size(L,1);
end
% // Convert x and y to an index for easy comparison with the image
% // We sort them so that we are comparing the same pixels
fittedInd = sort(sub2ind(size(L),y,x)).';
objInd = sort(sub2ind(size(L),objy,objx));
% Calculate the similarity. Intersect returns unique entities so we
% use unique on fittedInd
fitrate = numel(intersect(fittedInd,objInd)) ./ numel(unique(fittedInd));
if (fitrate >= th)
L(objInd) = 0;
processed_image(objInd) = 0;
% // figure(1),imshow(processed_image)
end
end
% // Display the result
figure,imshow(image);title('Original');
figure,imshow(processed_image);title('Processed image');
You could use J. E. Bresenham's algorightm. It is implemented by A. Wetzler in the following matlab function, which I tested myself.
The algorithm will give you the pixel coordinates of where the line would be, given that you will provide the start and end point of the line, which is already given in lines in your code above.
Here is the code I used, which uses the matlab function referenced above:
%This is your code above ========
image=imread('hcphc.png');
image = im2bw(image);
BW=edge(image,'canny');
imshow(BW);
figure,imshow(BW);
[H,T,R] = hough(BW);
P = houghpeaks(H,100,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
% =========
% Proposed solution:
% This will work for as many lines as you detected
for k=1:length(lines)
% Call Bresenham's algorithm
[x, y] = bresenham(lines(k).point1(1), lines(k).point1(2), ...
lines(k).point2(1), lines(k).point2(2));
% This is where you replace the line, here I use 0, but you can use
% whatever you want. However, note that if you use BW, you should only
% replace with 0 or 1, because is a logical image. If you want to use
% the original image, well, you know what to do.
BW(y, x) = 0;
% And now watch the lines disapear! (you can remove this line)
imagesc(BW), drawnow; pause(1);
end
Remember, download the matlab function first.

I want to color code a satellite trajectory plot using Matlab

I am creating a plot in Matlab that maps the azimuth and elevation of several GPS satellites over time. Below is an example of an image of the plotted GPS satellite trajectories.
However, I also want to plot the S_4 index (scintillation effect caused by the ionosphere) on the same plot. I want to color code each of the GPS satellite paths to show a high S_4 index as red, and a low S_4 index as blue. I have data for all of this, but I cannot figure out a way to color code each GPS path.
Currently I know the position of each satellite at every second of a day (this is how I am able to plot the trajectory). I also have S_4 index values at different times and for different satellites (the S_4 index matrix is not the same size as the satellite position matrix).
Any ideas?
This is the function I used. It uses as input a matrix called plotMat:
plotMat=[svID(satellite id number), gpsSec, elevation(radians), azimuth(radians)]
My function:
% find out from 'plotMat' if plotting satellite locations or trajectories in
% addition determine how many satellites are being tracked and how many
% samples for each satellite (# samples / satellite must always be equal)
gpsTime = plotMat(1,2);
i = 1;
t = gpsTime;
while ((i ~= size(plotMat,1)) & (t == gpsTime))
i = i + 1;
t = plotMat(i,2);
end
if (t == gpsTime)
sats = i;
else
sats = i - 1;
end;
samples = size(plotMat,1) / sats;
SVs = plotMat(1:sats,1);
elevation = plotMat(:,3);
azimuth = plotMat(:,4);
% initialize polar - plotting area
figure(10);clf;
axis([-1.4 1.4 -1.1 1.1]);
axis('off');
axis(axis);
hold on;
% plot circular axis and labels
th = 0:pi/50:2*pi;
x = [ cos(th) .67.*cos(th) .33.*cos(th) ];
y = [ sin(th) .67.*sin(th) .33.*sin(th) ];
plot(x,y,'color','w');
text(1.1,0,'90','horizontalalignment','center');
text(0,1.1,'0','horizontalalignment','center');
text(-1.1,0,'270','horizontalalignment','center');
text(0,-1.1,'180','horizontalalignment','center');
% plot spoke axis and labels
th = (1:6)*2*pi/12;
x = [ -cos(th); cos(th) ];
y = [ -sin(th); sin(th) ];
plot(x,y,'color','w');
text(-.46,.93,'0','horizontalalignment','center');
text(-.30,.66,'30','horizontalalignment','center');
text(-.13,.36,'60','horizontalalignment','center');
text(.04,.07,'90','horizontalalignment','center');
% plot titles
if (samples == 1)
title('Satellite Position Plot');
subtitle = sprintf('GPS time : %.2f sec',plotMat(1,2));
else
title('Satellite Trajectory Plot');
subtitle = sprintf('GPS time range : %.2f sec to %.2f sec', ...
plotMat(1,2),plotMat(size(plotMat,1),2));
text(-1.6,1,'SVID/Last Position','color','r');
text(-1.6,.9,'Positive Elevation','color','y');
text(-1.6,.8,'Negative Elevation','color','b');
end
text(0,-1.3,subtitle,'horizontalalignment','center');
% plot trajectories (or positions) and label the last postion with the
% satellite SV id; in addition, last postions are in red, otherwise position
% elevations are yellow and negative elavations are blue
%
% loop through samples
for s = 1:samples
% plot each satellite location for that sample
for sv = 1:sats
% check if positive or negative elevation
if (elevation((s - 1) * sats + sv) < 0)
elNeg = 1;
else
elNeg = 0;
end
% convert to plottable cartesian coordinates
el = elevation((s - 1) * sats + sv);
az = azimuth((s - 1) * sats + sv);
x = (pi/2-abs(el))/(pi/2).*cos(az-pi/2);
y = -1*(pi/2-abs(el))/(pi/2).*sin(az-pi/2);
% check for final sample
if (s == samples)
plot(x,y,'r*');
text(x,y+.07,int2str(SVs(sv)), ...
'horizontalalignment', ...
'center','color','r');
else
% check for +/- elevation
if (elNeg == 0)
plot(x,y,'y.');
else
plot(x,y,'b.');
end
end
end
end
axis equal;
On the previous code I haven't added the S_4 index. The following code compares the GPS seconds on the scintillation (S_4) data with the GPS seconds with the satellite position data. The matrix scint.mat refers to the scintillation data, while txinfo.mat refers to the satellite position data. This code creates a combination code that agrees on the GPS second and satellite identification number.
load('scint_324_first')
scint = scint';
load('txinfo_324_first_wrw')
txinfo = txinfo';
k = 0;
for i = 1:length(scint)
disp(num2str(i))
for j = 1:length(txinfo)
if scint(i,2) == txinfo(j,2) && scint(i,15) == txinfo(j,8)
k = k+1;
combo(k,:) = [scint(i,:) txinfo(j,:)];
end
end
end
If the S_4 values change over time, shall the colour coding change within single lines?
It's maybe not the most efficient way, but you could draw just lines for each piece of the orbits. Like
s4code = [1 0 0; 0 0 1]; % blue & red
for i = time_vector
if s4(i) > something
s = 2
else
s = 1
end
line(x(i:(i+1)), y(i:(i+1)), 'color', s4code(s, :))
end
About orbit data and s4 data not having the same size ... make them have the same size ... interp1, for example ;-)
If you are plotting using a polar plot, try instead 3D Polar Plot (FEX code by Ken Garrard)
If you are using plot, try Colored line or scatter plot or cline.m (again FEX files)
the last plot was done by:
x = 1:300
color_line(x,sin(x/20),sin(x/20))