Appending Int to an Array Matlab

조회 수: 2 (최근 30일)
Trevor
Trevor 2013년 3월 9일
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

채택된 답변

Cedric
Cedric 2013년 3월 9일
편집: Cedric 2013년 3월 9일
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
Trevor
Trevor 2013년 3월 10일
I got it all worked out thanks a lot!
Cedric
Cedric 2013년 3월 10일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by