如何找到在Matlab中完成某些结构的命令? [英] How to find the command by which certain struct done in Matlab?

查看:75
本文介绍了如何找到在Matlab中完成某些结构的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设a被定义为以下结构.我试图找到与R的dput等效的命令,但在这里.例如,我知道下面的内容必须为struct('const',1,'terms',{{struct(),struct()}})格式,但不知道是否要使用

Suppose a is defined as the struct below. I tried to find equivalent command to R's dput but errs here. For example, I know that the below must be of the form struct('const',1,'terms',{{struct(),struct()}}) but I don't know what is stored inside the structure without going to check it with commands like here, time-consuming.

那么,通过哪条命令我可以看到在Matlab中生成结构的原始命令?

>> a

a =

    const: 1
    terms: {[1x1 struct]  [1x1 struct]}

推荐答案

评论

您是否可以从使用结构切换到类?如果是这样,您可以制作一个模仿该结构的结构,每次对其进行修改时,调用stack = dbstack来获取堆栈-然后将堆栈与更改一起存储.稍后,可以从堆栈中的行号自动检索进行更改的命令.

作为注释中对此的后续请求,这是一个提供结构功能并记录其分配记录的类的示例:

As a follow-up request to this in the comment, here is an example of a class that provides struct functionality and also keeps a record of its assignments:

classdef utstruct
    properties (SetAccess = private)
        modifications
    end

    properties (Dependent, SetAccess = private)
        myStruct
    end

    properties (Access = private)
        m_struct
    end

    methods
        function self = utstruct(varargin)
            if nargin > 0
                self.m_struct = builtin('struct', varargin{:});
            else
                self.m_struct = builtin('struct');
            end
            % Should update self.modifications here
        end

        function B = subsref(self, s)
            if any(strcmp(s(1).subs, properties(self)))
                B = builtin('subsref', self, s);
            else
                B = subsref(self.m_struct, s);
            end
        end

        function A = subsasgn(self, s, b)
            self.m_struct = subsasgn(self.m_struct, s, b);

            newMod = builtin('struct');
            newMod.type = 'subsasgn';
            newMod.modData = {s b};
            newMod.stack = dbstack;
            self.modifications = [self.modifications; newMod];

            A = self;
        end

        function disp(self)
            disp(self.m_struct);
        end

        function names = fieldnames(self, varargin)
            names = fieldnames(self.m_struct, varargin{:});
        end

        function C = cat(self, dim, varargin)
            uts = cellfun(@(x)isa(x, 'utstruct'), varargin);
            varargin{uts} = cellfun(@(x)x.m_struct, varargin(uts));
            varargin = [{self.m_struct} varargin];
            self.m_struct = cat(dim, varargin{:});

            % Should update self.modifications here

            C = self;
        end

        function C = horzcat(self, varargin)
            C = self.cat(1, varargin{:});
        end

        function C = vertcat(self, varargin)
            C = self.cat(2, varargin{:});
        end

        function value = get.myStruct(self)
            value = self.m_struct;
        end
    end
end

当发生初始化/串联操作时,您应该添加一些代码来更新Modifys数组.

You should add some code to update the modifications array when initialisation / concatenation operations occur.

subsrefsubsasgn替代是使它像结构一样工作的关键点(通过将它们的所有活动推迟到实际的结构中),但是其他替代(例如fieldnamesdisp)也具有相同的功能事物.在subsasgn中,保留了对该结构的所有分配以及生成该分配的堆栈的记录.

The subsref and subsasgn overrides are the key points here that make it behave like a struct (by deferring all their activity to an actual struct), but other overrides like fieldnames and disp do the same thing. In subsasgn a record of all the assignments to the struct is kept, along with the stack that generated the assignment.

注意:为使其与内置struct完全兼容,您可能应该重写一些其他方法,但这足以使您入门.请参见对MATLAB内置类型进行子类化.

Note: for this to be fully compatible with the built-in struct you probably should override a few more methods, but this should be enough to get you started. See Subclassing MATLAB Built-In Types.

编辑:我使示例更加强大.现在,它是一个值类(应该是),并且可以串联使用.

I made the example a bit more robust. It's now a value class - as it should be - and works with concatenation.

您可以通过重新定义函数struct来避免使用查找替换重构现有的struct(...)调用:

You can avoid using a find-and-replace to refactor existing struct(...) calls by redefining the function struct:

function s = struct(varargin)
% STRUCT    Overrides default struct function to provide unit-testable structs
%
%   Set global variable unitTestStructEnabled to true to enable this
%   function.
%
global unitTestStructEnabled;

if isempty(unitTestStructEnabled)
    unitTestStructEnabled = false;
end

if unitTestStructEnabled
    s = utstruct(varargin{:});
else
    s = builtin('struct', varargin{:});
end

您可能不希望它一直挂在路径上,因为当您首次创建结构时会收到警告(您可以将其关闭,但这可能会隐藏其他问题),因此您应该将其放在通常不在路径中的文件夹中,然后暂时将其添加到用于单元测试的路径中(addpath/rmpath).

You probably don't want that hanging around on your path the whole time, as you will get a warning when you first create a struct (you could turn it off, but that might hide other problems), so you should probably put it in a folder that's not normally in the path, and temporarily add it to the path for unit testing (addpath / rmpath).

这篇关于如何找到在Matlab中完成某些结构的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆