【发布时间】:2015-04-10 03:04:13
【问题描述】:
我已经编写了一些 matlab 代码,但我遇到了一个问题,因为它没有给出所需的答案..
当我跑步时
track = readTrack('test1.gpx');
disp(track);
我以
的形式得到答案1x4 struct array containing the fields:
name
points
但理想情况下,我应该得到类似的测试
包含字段的标量结构:
name = Test #1
points =
0.00000 0.00000 0.00000
0.00000 2.00000 2.10000
0.00000 7.00000 4.50000
0.00000 10.00000 10.00000
关于如何更改代码以执行此操作以及如何更改的任何建议?
谢谢!! :)
这是我的代码。
function data = readTrack(filename)
%creates a struct with fields 'name' and 'points' from data extracted
%from gpx file. it uses supporting functions.
fid=fopen(filename);
if fid == -1
result = error (filename);
else
[data,name,point] = setUp(fid);
while length(point) > 9
[pointStruct, point] = findLatLongElev(point,fid,name);
data = [data pointStruct];
end
end
end
function result = error(filename)
% displays error message and produces empty struct
sprintf('File ''%s'' not found.',filename);
result = struct();
end
function [data,name,point] = setUp(fid)
%set up function, finds the name and gets to right place to find the
%points
data=[];
lineSkip(3,fid);
name = findNameOrElevation(fid);
lineSkip(13, fid);
point = fgetl(fid);
end
function lineSkip(n, fid)
%skips lines
for i=1:n
fgetl(fid);
end
end
function result = findNameOrElevation(fid)
% finds the name, however also used to find elevation as data presented
% in similar formats
line = fgetl(fid);
findStart = strfind(line,'>');
findFinish = strfind(line,'<');
printStart = findStart(1)+1;
printFinish = findFinish(2)-1;
result = line(printStart:printFinish);
end
function [pointStruct, point] = findLatLongElev(point,fid,name)
% finds latitude, longitude and elevation and creates a struct field
% entry for each set of points
apostrophes = strfind(point, '"');
latStart = apostrophes(1)+1;
latFinish = apostrophes(2)-1;
longStart = apostrophes(3)+1;
longFinish = apostrophes(4)-1;
trackPoint(1) = str2double(point(latStart:latFinish));
trackPoint(2) = str2double(point(longStart:longFinish));
trackPoint(3)= str2double(findNameOrElevation (fid));
fgetl(fid);
point = fgetl(fid);
pointStruct = struct(...
'name',name,...
'points', trackPoint);
end
【问题讨论】:
-
你的代码正在创建
structs,所以我不明白为什么你会认为结果不是struct。试试track.name和track.points而不是name和points。 -
抱歉,我看不出需要标量结构。您能否给出与当前输出相比与标量结构的关系。
-
需要作为对代码的要求,它必须在测试时给出!
-
@SamuelMetcalfe 好的,但我仍然不知道标量结构与结构数组的关系,这就是为什么我的答案是猜测。您能否尝试给出预期输出和结构数组之间的关系。此外,由于我的回答显然已经足够好,您应该尝试更好地解释您的需求。答案提供了 2 种方法来解决您的问题中出现的问题。你能解释一下为什么这不能解决问题吗?