Can you please explain why do we do this code?
1 次查看(过去 30 天)
显示 更早的评论
%Get antenna pairs and convert to matlab matrix
ants_pairs = vtrigU.vtrigU.GetAntennaPairs(mySettings.txMode);
TxRxPairs = zeros(ants_pairs.Length,2);
for ii = 1: ants_pairs.Length
TxRxPairs(ii,1) = double(ants_pairs(ii).tx);
TxRxPairs(ii,2) = double(ants_pairs(ii).rx);
end
0 个评论
回答(2 个)
dpb
2022-5-31
To illustrate how NOT to write MATLAB code, apparently...
The person responsible for this wanted a Nx2 vector of the two members of the stuct variable; what use is made of them later is up to whatever the unshown code does later on.
But, the same end is obtained "the MATLAB way" without a for..end loop and doesn't need the preallocation step, either --
%Get antenna pairs and convert to matlab matrix
ants_pairs = vtrigU.vtrigU.GetAntennaPairs(mySettings.txMode);
TxRxPairs=double([ants_pairs.tx ants_pairs.rx]);
The double is needed only if the members in the struct are some other data type and then may still not be any need to change them from their default data type; again there's no way to judge the rest of the code.
4 个评论
dpb
2022-6-1
编辑:dpb
2022-6-1
Can't tell without knowing what the rest of the code does with those values -- as noted, it may not make any difference at all; I don't know that the values in the original data struct aren't already double.
The default data type in MATLAB is the double; historically the other numeric classes were supported minimally if at all with math operations. It's possible this code dates from some time "way back when" before the other numeric types were more widely supported as now.
But, that's all conjecture and there's nothing so far that gives any hints as to there being a "why". With the initial code, the author clearly was not experienced in MATLAB so it may be only another symptom of that inexperience.
Walter Roberson
2022-6-2
The manufacturer's example code uses double() at that point, but does not explain why.
When using ActiveX controls (or when using Java methods), it is common that return values are in some internal array format that makes sense to the ActiveX control, and that the control supplies a double() method to convert to MATLAB double array.
For example this particular ActiveX control is written in C#. When it returns a C# array to MATLAB, it might be a header together with a block of memory in Column Major order, or in Row Major order, or it might be a header and a List, each element of which is a List of numbers that is a row or a column... all kinds of different ways that a program might store an array of data. The double() is not necessarily being used to convert between (for example) unsigned 16 bit integer and double precision: the data might already be double precision internally but arranged in a different way than MATLAB arranges data, so double() might be there to signal conversion of internal array format.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 ActiveX 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!