Advertisement
Guest User

Maplab.m

a guest
Dec 19th, 2013
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.97 KB | None | 0 0
  1. % Final Project - Maplab
  2.  
  3. % This is a simple mapping program. It is a function that receives a
  4. % location as an argument, loads and displays a map of Case in a GUI,
  5. % searches a cell array of location names, finds the corresponding index
  6. % in an array of coordinates, locates the coordinate on the image, and
  7. % displays the coordinates using a small yellow rectangle. It also displays
  8. % various maps of Case throughout history using a "time travel" button.
  9.  
  10. % SOURCE for campus maps: http://www.case.edu/its/archives/Buildings/campusmain.htm
  11.  
  12. function Maplab(X) % X tells us what location the user wants to see.
  13. fid = fopen('coords.csv', 'r'); % Opens the file to be read.
  14. data = textscan(fid, '%s %d %d', 'delimiter', ','); % Reads the file as one column of names, one column of coordinate matrices.
  15. fclose(fid); % Recloses the file.
  16. 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.
  17.     name = data{1}{i}; % Temporarily names name.
  18.     locX = data{2}(i); % Temporarily names location.
  19.     locY = data{3}(i);
  20.     location(i) = struct('Name', name, 'xCoord', locX, 'yCoord', locY); % Categorizes name and location into struct array fields.
  21. end
  22.  
  23. pic = imread('caseMap.png'); % Reads map of Case (pic).
  24. f = figure('Position', [1 100 1000 800]); % Creates GUI.
  25. title = uicontrol('Style', 'text', 'Position', [275 750 500 30], 'String', 'MAPLAB: An Interactive Map of CWRU', 'FontSize', 14);
  26.  
  27. locationList = uicontrol('Style', 'text', 'Position', [850 755 150 20], 'String', 'Location List', 'FontSize', 12);
  28. for i = 1:27
  29.     position = 740 - 20 * (i);
  30.     locationList = uicontrol('Style', 'text', 'Position', [850 position 150 20], 'String', data{1}(i));
  31. end
  32.  
  33. frame = uicontrol('Style', 'frame', 'Position', [0 300 200 300]);
  34. updateText = uicontrol('Style', 'text', 'Position', [25 500 150 50], 'String', 'Enter a new location (use underscores instead of spaces)');
  35. updateBox = uicontrol('Style', 'edit', 'Position', [25 425 150 50]);
  36. updateButton = uicontrol('Style', 'pushbutton', 'Position', [50 350 100 50], 'String', 'Update', 'FontSize', 14, 'Callback', @updateCallback);
  37. bigRedButton = uicontrol('Style', 'pushbutton', 'Position', [275 20 500 50], 'String', 'Time Travel', 'FontSize', 16, 'Callback', @useTheButton);
  38. timeTravelSlider = uicontrol('Style', 'slider', 'Position', [275 20 500 50], 'Visible', 'off', 'Min', 1, 'Max', 12, 'Callback', @timeTravelCallback);
  39. rightLim = uicontrol('Style', 'text', 'Position', [200 40 50 20], 'String', '1886', 'Callback', @futureCallback, 'Callback', @useTheButton, 'Visible', 'off');
  40. leftLim = uicontrol('Style', 'text', 'Position', [800 40 50 20], 'String', 'Present', 'Callback', @futureCallback, 'Callback', @useTheButton, 'Visible', 'off');
  41. imshow(pic) % Places Case map (pic) into GUI.
  42. for i = 1:27
  43.     name = location(i).Name; % This loop temporarily names the name and coordinates of each location in each iteration,
  44.     locX = location(i).xCoord;
  45.     locY = location(i).yCoord;
  46.     if strcmp(name, X) == 1 % checks if the name and argument location X are the same,
  47.         coordX = locX;
  48.         coordY = locY; % and, if they're the same, permanently names the coordinates of that location.
  49.     end
  50. end
  51. 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.
  52.  
  53.     function futureCallback(object,eventdata)
  54.         frame = uicontrol('Style', 'frame', 'Position', [0 300 200 300]);
  55.         updateText = uicontrol('Style', 'text', 'Position', [25 500 150 50], 'String', 'Enter a new location (use underscores instead of spaces)');
  56.         updateBox = uicontrol('Style', 'edit', 'Position', [25 425 150 50]);
  57.         updateButton = uicontrol('Style', 'pushbutton', 'Position', [50 350 100 50], 'String', 'Update', 'FontSize', 14, 'Callback', @updateCallback);
  58.         set(rectangle, 'Visible', 'on');
  59.         set([rightLim leftLim], 'Visible', 'off');
  60.         bigRedButton = uicontrol('Style', 'pushbutton', 'Position', [275 20 500 50], 'String', 'Time Travel', 'FontSize', 16, 'Callback', @useTheButton);
  61.         imshow('caseMap13.jpg')
  62.     end
  63.  
  64.     function useTheButton(object, eventdata)
  65.         for i = 1:9
  66.             uicontrol('Style', 'text', 'Position', [(300 + 50 * (i - 1)) 70 50 20], 'String', sprintf('%.0f', 1886 + 12.7 * i), 'Callback', @futureCallback)
  67.         end
  68.         bigRedButton = uicontrol('Style', 'pushbutton', 'Position', [275 20 500 50], 'String', 'Time Travel', 'Callback', @useTheButton, 'Visible', 'off');
  69.         set([rightLim leftLim], 'Visible', 'on');
  70.         backToTheFuture = uicontrol('Style', 'pushbutton', 'Position', [0 300 200 300], 'String', 'Back to the Future', 'FontSize', 16, 'Callback', @futureCallback);
  71.         timeTravelSlider = uicontrol('Style', 'slider', 'Position', [275 20 500 50], 'Visible', 'on', 'Min', 1, 'Max', 13, 'Value', 1, 'Callback', @timeTravelCallback);
  72.     end
  73.        
  74.     function timeTravelCallback(object, eventdata)
  75.         set(rectangle, 'Visible', 'off')
  76.         time = get(timeTravelSlider, 'Value');
  77.         picFile = sprintf('caseMap%.0f.jpg', time);
  78.         pic = imread(picFile);
  79.         imshow(pic)
  80.     end
  81.  
  82.     function updateCallback(object, eventdata)
  83.         newLocation = get(updateBox, 'String');
  84.         for i = 1:27
  85.             name = location(i).Name; % This loop temporarily names the name and coordinates of each location in each iteration,
  86.             locX = location(i).xCoord;
  87.             locY = location(i).yCoord;
  88.             if strcmp(newLocation, name) == 1 % checks if the name and argument location X are the same,
  89.                 coordX = locX;
  90.                 coordY = locY; % and, if they're the same, permanently names the coordinates of that location.
  91.             end
  92.         end
  93.         set(rectangle, 'Position', [coordX coordY 20 20])
  94.     end
  95.  
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement