"Reference to non-existent field" Error

1 回表示 (過去 30 日間)
Heba Sakr
Heba Sakr 2019 年 9 月 8 日
コメント済み: Guillaume 2019 年 9 月 9 日
I get this error (Reference to non-existent field 'system'.) when I use this line to call "generate_channel_cell" function
H_cell= generate_channel_cell( system ) ;
The fuction code is as following:
function channel_cell = generate_channel_cell(system)
p = inputParser ;
p.FunctionName = 'generate_channel_cell' ;
p.addRequired ('system') ;
Nt = round( p.Results.system.Nt ) ;
Nr = round( p.Results.system.Nr ) ;
Ns = round( p.Results.system.Ns ) ;
K = round( p.Results.system.K ) ;
coeff_size = [K * Nr , K * Nt ] ;
coeffiecients = (randn(coeff_size ) + 1j .* randn( coeff_size ) ) ;
coeffiecients = (1 / sqrt ( 2 ) ) .* coeffiecients ;
Nr_values = repmat ([ Nr ] , 1 , K ) ;
Nt_values = repmat ([ Nt ] , 1 , K ) ;
channel_cell = mat2cell( coeffiecients , Nr_values , Nt_values ) ;
end % end function

採用された回答

Guillaume
Guillaume 2019 年 9 月 8 日
編集済み: Guillaume 2019 年 9 月 8 日
Well, yes, you never use the input parser to parse your input, so the Results never get populated.
p.parse(system);
after you've created the parser but before you use the Results.
edit: on the other hand, your input parser is not very useful, it only checks that one input has been passed but you'll get an error beforehand if it's not the case. You don't check that the input is a structure with the correct fields
You'd be better off with:
function channel_cell = generate_channel_cell(system)
assert(isstruct(system) && all(ismember(fieldnames(system), {'Nt', 'Nr', 'Ns', 'K'}), 'system must be a structure with fields Nt, Nr, Ns and K');
Nt = round( p.Results.system.Nt ) ;
%...
  2 件のコメント
Heba Sakr
Heba Sakr 2019 年 9 月 8 日
Hi Guillaume, I appreciate your answer.
I understood the part of never using the input parser to parse the inputs, however, your code suggestion did't work for me.
I tried to modify the code as following, the code runs without any errors giving the expected results.
function channel_cell = generate_channel_cell(system)
% p = inputParser ;
% p.FunctionName = 'generate_channel_cell' ;
% p.addRequired ('system') ;
% Nt = round( p.Results.system.Nt ) ;
% Nr = round( p.Results.system.Nr ) ;
% Ns = round( p.Results.system.Ns ) ;
% K = round( p.Results.system.K ) ;
Nt = round(system.Nt ) ;
Nr = round(system.Nr ) ;
Ns = round(system.Ns ) ;
K = round(system.K ) ;
Guillaume
Guillaume 2019 年 9 月 9 日
Oops, yes, what I wrote was partly nonsense since I used the input parser that no longer existed. My answer should have read:
function channel_cell = generate_channel_cell(system)
assert(isstruct(system) && all(ismember(fieldnames(system), {'Nt', 'Nr', 'Ns', 'K'}), 'system must be a structure with fields Nt, Nr, Ns and K');
Nt = round(system.Nt);
%...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by