MATLAB - Difficulty in getting desired output with Struct -


i have matlab code i've written i'm having problem doesn't give required answer..

when run

track = readtrack('test1.gpx'); disp(track); 

i answer in form of

1x4 struct array containing fields:  name points 

but ideally test should like

scalar structure containing fields:

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 

any suggestions how change code , how?

thanks!! :)

here code.

function data = readtrack(filename)     %creates struct fields 'name' , 'points' data extracted     %from gpx file. 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 , produces empty struct     sprintf('file ''%s'' not found.',filename);     result = struct(); end  function [data,name,point] = setup(fid)     %set function, finds name , gets right place find     %points     data=[];     lineskip(3,fid);     name = findnameorelevation(fid);     lineskip(13, fid);     point = fgetl(fid); end   function lineskip(n, fid)     %skips lines     i=1:n         fgetl(fid);     end end  function result = findnameorelevation(fid)     % finds name, used find elevation 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 , elevation , creates struct field     % entry 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 

i hope can relationship between scalar struct , stuct array right here. guess 1 row in scalar field supposed correspond 1 element same field struct array. question edited make clearer if correct. answer,

in function readtrack explicitly state want have struct array.

while length(point) > 9     [pointstruct, point] = findlatlongelev(point,fid,name);     data = [data pointstruct]; % <--here! adds element array of structs. end 

to have scalar struct can catenate each element instead.

while length(point) > 9     [pointstruct, point] = findlatlongelev(point,fid,name);     data.name = [data.name; mat2cell(pointstruct.name,1,length(pointstruct.name))];     data.point = [data.point; pointstruct.point]; end 

however, need convert name cell since strings not of same length , should in cases* not assumed such. need done in setup

 data={}; 

i however, guess code more readable if rathered kept current output catenated fields afterwards in separate function. can done each field vertcat. however, still "matrix dimensions not consistent" error unless strings of same length or field data.name cell. can done loop since field names can obtained function fieldnames.

* exception may if string used represent else, eg. 8 characters can used represent 64-bit integer.


Popular posts from this blog