如何在 Scilab 上重载插入功能时使用“6”字符代码?

How to use the "6" char code in overloading insertion function on Scilab?

在 Scilab 6.0.2 的帮助文档中,我可以阅读以下关于 Overloading 条目的说明,关于该条目中显示的最后一个操作代码 "iext" table:

"The 6 char code may be used for some complex insertion algorithm like x.b(2) = 33 where b field is not defined in the structure x. The insertion is automatically decomposed into temp = x.b; temp(2) = 33; x.b = temp. The 6 char code is used for the first step of this algorithm. The 6 overloading function is very similar to the e's one."

但我找不到关于如何使用此 "char 6 code" 重载函数的完整示例。我正在尝试使用它,但没有成功。有没有人有关于如何执行此操作的示例?

下面的代码创建了一个正常的 "mlist" 作为例子。需要重载函数

A = rand(5,3)
names = ["colA" "colB" "colC"]
units = ["ft"   "in"     "lb"]
M = mlist(["Mlog" "names" "units" names],names,units,A(:,1),A(:,2),A(:,3))

以下是重载函数:

//define display
function %Mlog_p(M)
    n = size(M.names,"*")
    formatStr = strcat(repmat("%10s   ",1,n)) + "\n"
    formatNum = strcat(repmat("%0.10f   ",1,n)) + "\n"
    mprintf(formatStr,M.names)
    mprintf(formatStr,M.units)
    disp([M(M.names(1)),M(M.names(2)),M(M.names(3))])
end

//define extraction operation
function [Mat]=%Mlog_e(varargin)
    M    = varargin($)
    cols = [1:size(M.names,"*")] // This will also work
    cols = cols(varargin($-1))   // when varargin($-1) = 1:1:$
    Mat = []
    if length(varargin)==3 then
        for i = M.names(cols)
            Mat = [Mat M(i)(varargin(1))]
        end     
    else
        for i=1:size(M.names(cols),"*")
            Mat(i).name = M.names(cols(i))
            Mat(i).unit = M.units(cols(i))
            Mat(i).data  = M(:,cols(i))
        end
    end
endfunction

//define insertion operations (a regular matrix into a Mlog matrix)
function ML=%s_i_Mlog(i,j,V,M)
    names = M.names
    units = M.units
    A = M(:,:) // uses function above 
    A(i,j) = V
    ML = mlist(["Mlog" "names" "units" names],names,units,A(:,1),A(:,2),A(:,3))
endfunction

//insertion operation with structures (the subject of the question)
function temp = %Mlog_6(j,M)
    temp = M(j) // uses function %Mlog_e
endfunction

function M = %st_i_Mlog(j,st,M)
    A = M(:,:) // uses function %Mlog_e
    M.names(j) = st.name // uses function above
    M.units(j) = st.unit // uses function above
    A(:,j)     = st.data // uses function above  
    names = M.names
    units = M.units    
    M = mlist(["Mlog" "names" "units" names],names,units,A(:,1),A(:,2),A(:,3))
endfunction

第一个重载(显示mlist)将以如下形式显示矩阵table:

--> M
 M  = 

      colA         colB         colC   
        ft           in           lb   

   0.4720517   0.6719395   0.5628382
   0.0623731   0.1360619   0.5531093
   0.0854401   0.2119744   0.0768984
   0.0134564   0.4015942   0.5360758
   0.3543002   0.4036219   0.0900212

下一个重载(提取和插入)将允许 table 作为简单矩阵进行访问 M(i,j)

提取函数还将允许 M 被访问,其中returns一个结构,例如:

--> M(2)
 ans  =

   name: "colB"
   unit: "in"
   data: [5x1 constant]

最后两个函数就是问题中提到的重载。它们允许以结构形式更改列元数据。

--> M(2).name = "length"
 M  = 

      colA       length         colC   
        ft           in           lb   

   0.4720517   0.6719395   0.5628382
   0.0623731   0.1360619   0.5531093
   0.0854401   0.2119744   0.0768984
   0.0134564   0.4015942   0.5360758
   0.3543002   0.4036219   0.0900212