【问题标题】:Combining Matlab files using structs使用结构组合 Matlab 文件
【发布时间】:2017-10-22 09:19:49
【问题描述】:

所以,我是 Matlab 的初学者,在我的任务中需要一些帮助,因为我被困在这里! 基本上我有很多 Matlab 文件,它们使用不同的变量执行相同的功能,我需要将它们与一个函数组合在一个 Matlab 文件中,并保存这些不同的值以便稍后遍历它们并将它们传递给函数!

我一直在寻找适合保存变量的数据类型,最后我得到了结构体,所以我的结构体看起来像:

W = struct('Band7', {7, 1099, 236, 260, 236, 260, 0}, 
           'Band2', {2, 1078, 236, 300, 236, 300, 0},
           'Band3', {3, 1829, 236, 100, 236, 100, 0},
           'Band4', {4, 1367, 206, 500, 206, 500, 0},
           'Band1', {1, 1123, 246, 170, 246, 170, 0}, ...);

我的问题是:如何遍历结构中的每个波段以将其传递给这样的函数 -> RX_combined(W.Band4) 以及如何遍历函数本身内部的每个波段的值?!

根据 Wolfie 的回答,我将代码更新为:

function main 

% create a struct with the different vars
W = struct('Band7', {7, 1099, 236, 260, 236, 260, 0}, 
           'Band2', {2, 1078, 236, 300, 236, 300, 0},
           'Band3', {3, 1829, 236, 100, 236, 100, 0},
           'Band4', {4, 1367, 206, 500, 206, 500, 0},
           'Band1', {1, 1123, 246, 170, 246, 170, 0});

           fields = fieldnames(W)

% iterate over all bands and pass them to the function.
for i=1:numel(fields)
  fields(i)
  RX_combined(W.(fields{i}))
end 
end

我的问题是如何访问函数内每个波段的值?!


经过一些搜索和研究后,我发现 Matlab 假设我将数组的单元分布在结构数组元素中,而不是使用每个单元数组作为我想要的单元!指的是这个answer

为此,我通过向每个元胞数组添加另一个花括号来解决问题!

现在我的代码是:

function main 

% create a struct with the different values of the RX bands to merge the files in one!

W = struct('Band7',{{7, 1099, 236, 260, 236, 260, 0}},'Band2',{{2, 1078, 236, 300, 236, 300, 0}},'Band3',{{3, 1829, 236, 100, 236, 100, 0}},'Band4',{{4, 1367, 206, 500, 206, 500,0}},'Band1',{{4, 1367, 206, 500, 206, 500,0}});


fields = fieldnames(W);

% iterate over all the bands and pass them to the function.
for i=1:numel(fields)
  fields(i);
  %Wait for the User's keypress : this allows us to run every RX band file one by one
a = input('Run the new RX file (y/n)? ','s')
if strcmpi(a,'y')
  RX_combined( { W.(fields{i}) });
end 
end
end

function [] = RX_combined(band)

 P=int16([]);
numValues = numel(band);
for i = 1: numValues
    P.Band= band{i}{1}; 
    P.Channel_Frequency= band{i}{2}; 
    P.RD_GAIN_1= band{i}{3}; 
    P.RD_GAIN_ANA= band{i}{4};
    P.RX_GAIN_1= band{i}{5}; 
    P.RX_GAIN_ANA= band{i}{6}; 
    P.RX_ULP= band{i}{7}; 
end  
disp (P);
end    

【问题讨论】:

  • 您可以像f = fieldnames(W) 一样获取结构的字段名称,然后使用RX_combined(W.(f{ii})) 循环遍历。那是在字段名称周围使用括号,因为它是一个字符串,其中ii 是循环变量
  • @Wolfie 好的,这就是我将某个波段传递给函数的方式,但是在函数内部循环遍历波段的值呢?!
  • 您想在RX_combined 函数中执行此操作,还是只想将一个标量值传递给该函数?无论哪种方式,您都应该使用带方括号的数组/一维矩阵,而不是带花括号的元胞数组,因为您的数据是数字的。然后只需在第一个(或函数内)添加另一个循环来循环数组值
  • 如果这是代表你的数据,你根本不需要结构数组;二维数组会更快更容易。但是考虑到您组织数据的方式,@Wolfie 的建议只是将数据周围的大括号 {} 更改为方括号 [] 将解决您的问题。
  • 我只是想清楚我需要结构,因为每个波段本身都是一个单元,所以当我在函数“RX_combined”中遍历它时,我需要将值分配给 7 个不同的变量!所以如果我可以像在 C 中那样访问它,比如 'Band7.arg1= var1' ..etc

标签: matlab function struct


【解决方案1】:

以 Wolfie 和 beaker 的 cmets 为基础,以下是两种解决方案:一种使用最初提出的元胞数组样式,另一种使用数字数组而不是元胞数组(如果您是只希望您的示例中的数字数据)。

使用元胞数组:

function [] = myFunction()

% create a struct with the different vars
W = struct('Band7', {7, 1099, 236, 260, 236, 260, 0},... 
           'Band2', {2, 1078, 236, 300, 236, 300, 0},...
           'Band3', {3, 1829, 236, 100, 236, 100, 0},...
           'Band4', {4, 1367, 206, 500, 206, 500, 0},...
           'Band1', {1, 1123, 246, 170, 246, 170, 0});

           fields = fieldnames(W);

% iterate over all bands and pass them to the function.

for i=1:numel(fields)
  disp(fields(i))
  %RX_combined(   W.(fields{i})  ); %Would return each value seperately. Only
                                    %first value will be passed to function, 
                                    %producing undesirable behaviour
  RX_combined( { W.(fields{i}) }); %curly braces make output cell array
end



end

function [] = RX_combined(band)
numValues = numel(band);
for i = 1: numValues
    disp(band{i}); %replace 'disp' with action you want to perform
end
end

使用数值数组: (注意更少的花括号和其他奇怪的东西)

function [] = myFunction()

% create a struct with the different vars
W = [7, 1099, 236, 260, 236, 260, 0;... 
     2, 1078, 236, 300, 236, 300, 0;...
     3, 1829, 236, 100, 236, 100, 0;...
     4, 1367, 206, 500, 206, 500, 0;...
     1, 1123, 246, 170, 246, 170, 0];

rowNames = {'Band7', 'Band2', 'Band3', 'Band4', 'Band1'};

% iterate over all bands(rows) and pass them to the function.
for i=1:size(W,1) %numel(fields)
  disp(rowNames{i})
  RX_combined( W(i,:) ); %passes ith row, all columns
end
end

function [] = RX_combined(band)
numValues = numel(band);
for i = 1:numValues
    disp(band(i)); %'band' is now numeric array, not cell array 
                   %so round braces instead of curly ones
end
end

【讨论】:

  • 感谢您的回答,但我认为我还不够清楚每个波段本身就是一个单元,所以当我将它传递给函数时,我需要循环并将它们分配给 7 个变量函数!
  • 对。因此,要做到这一点,您将替换 RX_combined 中我编写了“disp(band(i))”的代码部分,它简化了显示字段的值,代码将带中的变量分配给您拥有的 7 个变量。为此,您可以使用“switch”语句(参见下面的链接)将值分配给适当的变量,使用循环变量“i”作为 switch 表达式。 mathworks.com/help/matlab/ref/switch.html
  • 我用最终的想法更新了我的问题!感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 2016-05-04
  • 2015-12-19
  • 2012-11-14
  • 2014-09-20
  • 1970-01-01
相关资源
最近更新 更多