Appending Int to an Array Matlab

2 views (last 30 days)
Trevor
Trevor on 9 Mar 2013
I am using an API to get real data times of trains and am trying to get the closest train time to a user entered time and then display that train time, and the next 4 granted the trains are running. I am reading in the information and the code goes through what its supposed to do but when I look at the array its a bunch of [] brackets in 7 cells instead of the calculated numbers. Any suggestions? Code is below with the API
if true
%this is the API link for the live data from Septa this will get 30
%results and see which time is closer to the user entered time
requestInfoSeptaLive = ['http://www3.septa.org/hackathon/NextToArrive/' requestStationSelected '/' requestEndStation '/30'];
%Again tries to get the information and if there is a failure it will give
%a probable cause and terminate the program
try
getInfoSeptaLive = urlread(requestInfoSeptaLive);
catch
if getInfoSeptaLive ~= '[]'
disp...
('Either the arrival/depart stations dont quite match up or theres a server error. Try again.');
return;
else
disp('Unable to fetch the information from Septa, please try again')
return;
end
end
%parses the information returned from the Live API
dataReturnedFromLiveAPI = parse_json(getInfoSeptaLive);
dataReturnedFromLiveAPI = dataReturnedFromLiveAPI{1};
%gets the size of the API in case there are no trains running
sizeOfDataNoTrains = size(dataReturnedFromLiveAPI, 1);
sizeOfData = size(dataReturnedFromLiveAPI, 2);
counter = 0;
for i = 1:sizeOfData
scanForClosestTime = dataReturnedFromLiveAPI{1,i}.orig_departure_time;
trainTimeGivenH = sscanf(scanForClosestTime, '%i');
findColonTrain = strfind(scanForClosestTime, ':');
trainTimeGivenMStr = scanForClosestTime(findColonTrain+1:4);
trainTimeGivenM = int32(str2num(trainTimeGivenMStr));
trainDepartTimeM = (trainTimeGivenH(1,1) * 60) + (trainTimeGivenM);
differenceBetweenTimes = trainDepartTimeM - userEnteredMins;
if trainDepartTimeM < userEnteredMins
differenceBetweenTimes = userEnteredMins - trainDepartTimeM;
end
stopAtEndOfData = sizeOfData;
goodTimeFrame = 60;
closestTime = cell(1, stopAtEndOfData);
storeTheDifference = cell(1, stopAtEndOfData);
if(differenceBetweenTimes < 60)
if (counter < 5)
closestTime{i} = scanForClosestTime;
storeTheDifference{i} = differenceBetweenTimes;
counter = counter + 1;
end
end
end
end

Accepted Answer

Cedric
Cedric on 9 Mar 2013
Edited: Cedric on 9 Mar 2013
Your stations are probably incorrect.. the following works:
>>requestInfoSeptaLive = 'http://www3.septa.org/hackathon/NextToArrive/Airport%20Terminal%20B/Ardmore/10' ;
>> getInfoSeptaLive = urlread(requestInfoSeptaLive)
getInfoSeptaLive = [{"orig_train":"9472","orig_line":"Airport","orig_departure_time":"11:12PM","orig_arrival_time":"11:30PM","orig_delay":"On time","term_train":"591","term_line":"Paoli\/Thorndale","term_depart_time":"12:19AM","term_arrival_time":"12:35AM","Connection":"30th Street Station","term_delay":"5 mins","isdirect":"false"},{"orig_train":"9472","orig_line":"Airport","orig_departure_time":"11:12PM","orig_arrival_time":"11:30PM","orig_delay":"On time","term_train":"1595","term_line":"Paoli\/Thorndale","term_depart_time":" 1:19AM","term_arrival_time":" 1:35AM","Connection":"30th Street Station","term_delay":"On time","isdirect":"false"},{"orig_train":"474","orig_line":"Airport","orig_departure_time":"11:42PM","orig_arrival_time":"12:00AM","orig_delay":"On time","term_train":"591","term_line":"Paoli\/Thorndale","term_depart_time":"12:19AM","term_arrival_time":"12:35AM","Connection":"30th Street Station","term_delay":"5 mins","isdirect":"false"},{"orig_train":"474","orig_line":"Airport","orig_departure_time":"11:42PM","orig_arrival_time":"12:00AM","orig_delay":"On time","term_train":"1595","term_line":"Paoli\/Thorndale","term_depart_time":" 1:19AM","term_arrival_time":" 1:35AM","Connection":"30th Street Station","term_delay":"On time","isdirect":"false"},{"orig_train":"476","orig_line":"Airport","orig_departure_time":"12:12AM","orig_arrival_time":"12:30AM","orig_delay":"On time","term_train":"1595","term_line":"Paoli\/Thorndale","term_depart_time":" 1:19AM","term_arrival_time":" 1:35AM","Connection":"30th Street Station","term_delay":"On time","isdirect":"false"}]
Do you urlencode station names? I.e. spaces should be converted into % 2 0, & should be converted into & a m p ;, etc ..
  9 Comments
Trevor
Trevor on 10 Mar 2013
I got it all worked out thanks a lot!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!