How to assign objects to array?

조회 수: 2 (최근 30일)
Jan
Jan 2023년 4월 17일
댓글: Mario Malic 2023년 4월 17일
% https://de.mathworks.com/help/rf/ref/sparameters.html
I'm using the RFToolbox and have a bunch of S-Parameters which I import from Touchstone files.
f = "path-to-touchstone-file.s2p";
sobj = sparameters(f);
sobj.Parameters
sobj.Frequencies
sobj.Impedance
Now I want to create an array which holds multiple objects and is accessible by index.
f = ["path-to-file-1",...
"path-to-file-2",...
"path-to-file-3"...
];
for i = 1:numel(f)
sobj(i) = sparameters(f(i));
end
sobj(1).Parameters
sobj(1).Frequencies
sobj(1).Impedance
This does not work. I don't know how to create an array, which can hold the objects returned from sparameters(). I get the following output:
% Unable to perform assignment because value
% of type 'sparameters' is not convertible to
% 'cell'.
%
% Error in example (line 7)
% sobj(i) = sparameters(f(1));
%
% Caused by:
% Error using cell
% Conversion to cell from sparameters is
% not possible.
If I create a single S-Parameter object first I'm able to add more objects afterwards, but I'd like to know how to preallocate the proper data structure.
f = ["path-to-file-1",...
"path-to-file-2",...
"path-to-file-3"...
];
sobj = sparameters(f(1));
for i:numel(f)
sobj(i) = sparameters(f(i));
end
  댓글 수: 5
Jan
Jan 2023년 4월 17일
Seems to work!
sobj = sparameters.empty;
or
sobj = sparameters.empty(numel(f), 0);
I don't know if the last line is a reasonable thing to do perfermance wise because the true size is still not fully specified. I can live with it for now, my datasets are only a few and small.
BUT it seems I did something wrong before because now the code in the third block works and the problem I described does not apply anymore¯\_(ツ)_/¯
Thank you very much for your time!
Mario Malic
Mario Malic 2023년 4월 17일
Yes, for some reason sobj was treated as cell array and you were indexing into it the wrong way. This would have worked.
for i:numel(f)
sobj{i} = sparameters(f(i));
end
You are welcome.

댓글을 달려면 로그인하십시오.

채택된 답변

Mario Malic
Mario Malic 2023년 4월 17일
It is possible to create an empty array of specified class using https://www.mathworks.com/help/matlab/ref/empty.html
sobj = sparameters.empty(numel(f), 0);
Don't worry about the performance if you did not preallocate array for it. You probably won't notice it even if numel(f) is 1000ish. You can verify yourself with tic and toc.
for i:numel(f)
sobj(i) = sparameters(f(i));
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Visualization and Data Export에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by