Borrar filtros
Borrar filtros

Can you please explain why do we do this code?

3 visualizaciones (últimos 30 días)
Ashis Pandey
Ashis Pandey el 31 de Mayo de 2022
Respondida: Walter Roberson el 2 de Jun. de 2022
%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

Respuestas (2)

dpb
dpb el 31 de Mayo de 2022
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 comentarios
dpb
dpb el 1 de Jun. de 2022
Editada: dpb el 1 de Jun. de 2022
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.
Ashis Pandey
Ashis Pandey el 1 de Jun. de 2022
Thankyou very much you seem very helpful, I really want to understand the code, I can send you the full info if you have the time.
Please let me know if possible, I can arrange any possible help like fund from my side to accommodate your time

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 2 de Jun. de 2022
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.

Categorías

Más información sobre Antennas, Microphones, and Sonar Transducers en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by