所以,我是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)
以及如何遍历函数本身内的每个band的值?!
根据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
答案 0 :(得分:1)
根据Wolfie和beaker的评论,下面是两个解决方案:一个使用最初提出的单元格数组样式,另一个使用数字数组而不是单元格数组(我喜欢烧杯,如果你&#39,我会推荐;只需要像你的例子那样只期待数字数据。)
使用单元格数组:
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