Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Final Project - Maplab
- % This is a simple mapping program. It is a function that receives a
- % location as an argument, loads and displays a map of Case in a GUI,
- % searches a cell array of location names, finds the corresponding index
- % in an array of coordinates, locates the coordinate on the image, and
- % displays the coordinates using a small yellow rectangle. It also displays
- % various maps of Case throughout history using a "time travel" button.
- % SOURCE for campus maps: http://www.case.edu/its/archives/Buildings/campusmain.htm
- function Maplab(X) % X tells us what location the user wants to see.
- fid = fopen('coords.csv', 'r'); % Opens the file to be read.
- data = textscan(fid, '%s %d %d', 'delimiter', ','); % Reads the file as one column of names, one column of coordinate matrices.
- fclose(fid); % Recloses the file.
- for i = 1:27 % This loop cycles through each name and coordinate pair in the text file and uses them to create a struct array.
- name = data{1}{i}; % Temporarily names name.
- locX = data{2}(i); % Temporarily names location.
- locY = data{3}(i);
- location(i) = struct('Name', name, 'xCoord', locX, 'yCoord', locY); % Categorizes name and location into struct array fields.
- end
- pic = imread('caseMap.png'); % Reads map of Case (pic).
- f = figure('Position', [1 100 1000 800]); % Creates GUI.
- title = uicontrol('Style', 'text', 'Position', [275 750 500 30], 'String', 'MAPLAB: An Interactive Map of CWRU', 'FontSize', 14);
- locationList = uicontrol('Style', 'text', 'Position', [850 755 150 20], 'String', 'Location List', 'FontSize', 12);
- for i = 1:27
- position = 740 - 20 * (i);
- locationList = uicontrol('Style', 'text', 'Position', [850 position 150 20], 'String', data{1}(i));
- end
- frame = uicontrol('Style', 'frame', 'Position', [0 300 200 300]);
- updateText = uicontrol('Style', 'text', 'Position', [25 500 150 50], 'String', 'Enter a new location (use underscores instead of spaces)');
- updateBox = uicontrol('Style', 'edit', 'Position', [25 425 150 50]);
- updateButton = uicontrol('Style', 'pushbutton', 'Position', [50 350 100 50], 'String', 'Update', 'FontSize', 14, 'Callback', @updateCallback);
- bigRedButton = uicontrol('Style', 'pushbutton', 'Position', [275 20 500 50], 'String', 'Time Travel', 'FontSize', 16, 'Callback', @useTheButton);
- timeTravelSlider = uicontrol('Style', 'slider', 'Position', [275 20 500 50], 'Visible', 'off', 'Min', 1, 'Max', 12, 'Callback', @timeTravelCallback);
- rightLim = uicontrol('Style', 'text', 'Position', [200 40 50 20], 'String', '1886', 'Callback', @futureCallback, 'Callback', @useTheButton, 'Visible', 'off');
- leftLim = uicontrol('Style', 'text', 'Position', [800 40 50 20], 'String', 'Present', 'Callback', @futureCallback, 'Callback', @useTheButton, 'Visible', 'off');
- imshow(pic) % Places Case map (pic) into GUI.
- for i = 1:27
- name = location(i).Name; % This loop temporarily names the name and coordinates of each location in each iteration,
- locX = location(i).xCoord;
- locY = location(i).yCoord;
- if strcmp(name, X) == 1 % checks if the name and argument location X are the same,
- coordX = locX;
- coordY = locY; % and, if they're the same, permanently names the coordinates of that location.
- end
- end
- rectangle = uicontrol('Style', 'frame', 'Position', [coordX coordY 20 20], 'BackgroundColor', 'yellow', 'Callback', @timeTravelCallback, 'Callback', @updateCallback, 'Callback', @futureCallback); % Finally, it places a yellow rectangle, of size 20 by 20 pixels, at the argument location.
- function futureCallback(object,eventdata)
- frame = uicontrol('Style', 'frame', 'Position', [0 300 200 300]);
- updateText = uicontrol('Style', 'text', 'Position', [25 500 150 50], 'String', 'Enter a new location (use underscores instead of spaces)');
- updateBox = uicontrol('Style', 'edit', 'Position', [25 425 150 50]);
- updateButton = uicontrol('Style', 'pushbutton', 'Position', [50 350 100 50], 'String', 'Update', 'FontSize', 14, 'Callback', @updateCallback);
- set(rectangle, 'Visible', 'on');
- set([rightLim leftLim], 'Visible', 'off');
- bigRedButton = uicontrol('Style', 'pushbutton', 'Position', [275 20 500 50], 'String', 'Time Travel', 'FontSize', 16, 'Callback', @useTheButton);
- imshow('caseMap13.jpg')
- end
- function useTheButton(object, eventdata)
- for i = 1:9
- uicontrol('Style', 'text', 'Position', [(300 + 50 * (i - 1)) 70 50 20], 'String', sprintf('%.0f', 1886 + 12.7 * i), 'Callback', @futureCallback)
- end
- bigRedButton = uicontrol('Style', 'pushbutton', 'Position', [275 20 500 50], 'String', 'Time Travel', 'Callback', @useTheButton, 'Visible', 'off');
- set([rightLim leftLim], 'Visible', 'on');
- backToTheFuture = uicontrol('Style', 'pushbutton', 'Position', [0 300 200 300], 'String', 'Back to the Future', 'FontSize', 16, 'Callback', @futureCallback);
- timeTravelSlider = uicontrol('Style', 'slider', 'Position', [275 20 500 50], 'Visible', 'on', 'Min', 1, 'Max', 13, 'Value', 1, 'Callback', @timeTravelCallback);
- end
- function timeTravelCallback(object, eventdata)
- set(rectangle, 'Visible', 'off')
- time = get(timeTravelSlider, 'Value');
- picFile = sprintf('caseMap%.0f.jpg', time);
- pic = imread(picFile);
- imshow(pic)
- end
- function updateCallback(object, eventdata)
- newLocation = get(updateBox, 'String');
- for i = 1:27
- name = location(i).Name; % This loop temporarily names the name and coordinates of each location in each iteration,
- locX = location(i).xCoord;
- locY = location(i).yCoord;
- if strcmp(newLocation, name) == 1 % checks if the name and argument location X are the same,
- coordX = locX;
- coordY = locY; % and, if they're the same, permanently names the coordinates of that location.
- end
- end
- set(rectangle, 'Position', [coordX coordY 20 20])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement