【问题标题】:Generate struct in matlab with fields of different sizes在matlab中生成具有不同大小字段的结构
【发布时间】:2017-08-05 12:11:48
【问题描述】:

我正在尝试生成有关一组图像的信息集合,因此我创建了一个结构数组,如下所示,

resultsInfo = struct('img_index',0,'correlated',cell(1,5),'correlationFactor',zeros(1,5),'ImgSum',zeros(640,480));

其中:img_index 是表示图像的整数,correlated 是包含相关图像名称的单元格,correlationFactor 是表示图像相似程度的数字,imgSum 是相关图像的总和。

我想在 for 循环内以动态方式创建数组,但代码仅生成每个元素的第五个 imageSums

如何启动结构以用零矩阵填充数组的所有元素?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    请定义以下方法:

    function resultStruct = CreateEmptySruct ()
        resultStruct.img_index = 0  ;
        resultStruct.correlated  = cell(1,5);
        resultStruct.correlationFactor = zeros(1,5);
        resultStruct.ImgSum = zeros(640,480) ;
    end
    

    然后像这样在 For 循环中调用这个方法:

    for i = 1 :5
        structArray(i) = CreateEmptySruct () ;
    end
    

    但是,您也可以通过传递函数参数为每个结构单独设置任何值。

    【讨论】:

    • 为我工作。谢谢。
    • 不客气。如果此解决方案对您有用,请接受答案。
    猜你喜欢
    • 2016-03-17
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    相关资源
    最近更新 更多