我使用的是API来获取列车的实际数据时间,并试图获取距离用户输入时间最近的列车时间,然后显示该列车时间,并且接下来的4个列车准许列车运行。我正在阅读信息,代码完成了它应该做的事情,但是当我在7个单元格中查看数组中的一堆[]括号而不是计算出的数字时。有什么建议么?下面的代码与API将Int附加到数组Matlab中
测试值: requestStationSelected = '大学%20City' 和requestEndStation = '罗斯林'
%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
您可以给'requestStationSelected'和'requestEndStation'有效的值,这样我们就可以看到问题是什么样子了?一个'[]'意味着一个空单元 - 通常,如果你先分配给{3}而没有先用{1}和{2}做某件事,你会得到{[],[],value}。它看起来像是在你分配'nearestTime {i}'的地方发生的,但是不能运行我猜测的代码。 – Floris 2013-03-09 00:54:14
requestStationSelected ='University%20City'and requestEndStation ='Roslyn' – heinst 2013-03-09 00:59:09