我正在使用API获取列车的实际数据时间,并且我正在尝试将最近的列车时间用于用户输入的时间,然后显示列车时间,接下来的4列车已经开始运行。我正在阅读信息,代码经历了它应该做的事情,但是当我看到数组时,7个单元格中的一堆[]括号而不是计算出的数字。有什么建议?代码如下,API
测试值: requestStationSelected ='大学%20City'和requestEndStation =' Roslyn'
%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
答案 0 :(得分:1)
您在for
循环中分配您的单元格数组:
for i = 1:sizeOfData
...
closestTime = cell(1, stopAtEndOfData);
storeTheDifference = cell(1, stopAtEndOfData);
...
end
这意味着在循环的每次迭代中将它们都转换为{[],[],[],[],[]...}
的数组 - 所以除非最后一次迭代中有一个有效的“最接近时间”,否则你的单元格数组将是所有空数组 - 如果是的话,除了最后一个元素之外的所有元素仍然是[]。
要解决此问题,请在开始for
循环之前将两行移至。
第二个问题似乎是存储结果的数组的索引。如果您只想要五个结果,我假设您要将它们存储在数组的元素1 - 5中,而不是存储在“任何”位置。我会将代码更改为
if (counter < 5)
counter = counter + 1;
closestTime{counter} = scanForClosestTime;
storeTheDifference{counter} = differenceBetweenTimes;
end
但也许我误解了你想怎么处理它?</ p>
与您的问题无关,您可能需要查看
行trainTimeGivenMStr = scanForClosestTime(findColonTrain+1:4);
这很可能不是你打算做的 - 看一下响应的例子,我找到了字符串"orig_departure_time":"11:57PM"
。我希望findColonTrain == 3
,以便上面的行成为
trainTimeGivenMStr = scanForClosestTime(4:4);
只是一个角色。也许你的意思是
trainTimeGivenMStr = scanForClosestTime(findColonTrain +(1:4));
会变成
trainTimeGivenMStr = scanForClosestTime(4:7);
这样
trainTimeGivenMStr = '57PM';
我希望这三件事能帮助你完成所有工作!
编辑:今天早上有机会运行你的代码 - 发现了许多其他问题。我在下面添加了带注释的“工作”代码:最大的问题很可能是您没有在代码中处理AM / PM。请注意,我使用了不同的json解析器 - 这稍微改变了几行。我相信你可以把它放回去以你想要的方式工作。这会在所有单元格中返回有效数据。dataReturnedFromLiveAPI = loadjson(getInfoSeptaLive);
% next line not needed - loadjson returns struct array, not cell array
%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;
stopAtEndOfData = sizeOfData;
closestTime = cell(1, stopAtEndOfData);
storeTheDifference = cell(1, stopAtEndOfData);
userEnteredMins = 12*60+30; % looking for a train around 12:30 pm
for ii = 1:sizeOfData
scanForClosestTime = dataReturnedFromLiveAPI(ii).orig_departure_time;
trainTimeGivenH = sscanf(scanForClosestTime, '%i');
% since we'll be considering AM/PM, have to set 12 = 0:
if (trainTimeGivenH == 12), trainTimeGivenH = 0; end
findColonTrain = strfind(scanForClosestTime, ':');
% change next line to get minutes plus AM/PM:
trainTimeGivenMStr = scanForClosestTime(findColonTrain+(1:4));
% look at just minutes:
trainTimeGivenM = int32(str2num(trainTimeGivenMStr(1:2)));
% adjust for AM/PM:
if(trainTimeGivenMStr(3:4)=='PM'), trainTimeGivenH = trainTimeGivenH+12; end;
% compute time in minutes:
trainDepartTimeM = (trainTimeGivenH * 60) + (trainTimeGivenM);
differenceBetweenTimes = trainDepartTimeM - userEnteredMins;
if trainDepartTimeM < userEnteredMins
differenceBetweenTimes = userEnteredMins - trainDepartTimeM;
end
% added a couple of lines to see what is happening:
fprintf(1, 'train %d: depart %s - in minutes this is %d vs user entered %d\n', ...
ii, scanForClosestTime, trainDepartTimeM, userEnteredMins);
goodTimeFrame = 60;
if(differenceBetweenTimes < 600)
if (counter < 10)
counter = counter + 1;
closestTime{counter} = scanForClosestTime;
storeTheDifference{counter} = differenceBetweenTimes;
end
end
end