【问题标题】:MATLAB - Difficulty in getting desired output with StructMATLAB - 使用 Struct 难以获得所需的输出
【发布时间】: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.nametrack.points 而不是namepoints
  • 抱歉,我看不出需要标量结构。您能否给出与当前输出相比与标量结构的关系。
  • 需要作为对代码的要求,它必须在测试时给出!
  • @SamuelMetcalfe 好的,但我仍然不知道标量结构与结构数组的关系,这就是为什么我的答案是猜测。您能否尝试给出预期输出和结构数组之间的关系。此外,由于我的回答显然已经足够好,您应该尝试更好地解释您的需求。答案提供了 2 种方法来解决您的问题中出现的问题。你能解释一下为什么这不能解决问题吗?

标签: matlab struct output


【解决方案1】:

我希望我可以在这里得到标量结构和结构数组之间的关系。猜测是标量字段中的一行应该对应于结构数组中同一字段中的一个元素。如果我是正确的,可以编辑该问题以使其更清楚。所以对于答案,

在函数readTrack 中,您实际上明确声明您想要一个结构数组。

while length(point) > 9
    [pointStruct, point] = findLatLongElev(point,fid,name);
    data = [data pointStruct]; % <--Here! Adds another element to an array of structs.
end

要拥有一个标量结构,您可以改为连接每个元素。

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

但是,您需要将名称转换为单元格,因为字符串不一定具有相同的长度,并且在大多数情况下*不应假定为这样。这也需要在setUp 中完成

 data={};

但是,我猜想如果您宁愿将当前输出与之后的字段连接在一个单独的函数中,那么代码将更具可读性。这可以使用vertcat 为每个字段完成。但是,除非所有字符串的长度相同或字段 data.name 是单元格,否则您仍然会收到“矩阵尺寸不一致”错误。这可以通过循环来完成,因为可以从函数fieldnames 中获取字段名称。

* 一个例外可能是如果一个字符串被用来表示别的东西,例如。 8个字符可用于表示一个64位整数。

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    相关资源
    最近更新 更多