Matlab get stuck when processing large bus

5 vues (au cours des 30 derniers jours)
Jaime Noguero
Jaime Noguero le 10 Juin 2024
Hello all,
I have a matlab function with a set of busses as inputs. The job of this matlab function is basically to act as a multiplexer. In other words, it has to select an specific bus field from one of the bus inputs and ouput its value. Selection is done using an address input.
In order to select specific bus field, I have a cell matrix that associates address and bus field name in each row.
I have a nested for loop that iterates over all addresses in cell matrix until it finds one that match with input address. Once corresponding row has been found, it iterates over all bus fields until it finds that one whose name matchs with the name stored in the matrix row.
It is something like:
function [output] = selector(bus1,bus2,inputAddress)
output=0; %Default value for output
for i:1:length(numberOfMatrixRows)
%Find address.
if(inputAddress==matrix{i,2}) % Address is stored in second column of matrix
%If address match, then look for corresponding bus field
%Look in bus1
fieldsBus1=fieldnames(bus1);
for u=1:length(fielsBus1)
if(bus1.fieldsBus1{u}==matrix{i,1}) %Field names are stored in first column of matrix
%If bus field name matchs with field name stored in matrix, select that field name value as output
output=bus1.fielsBus1{u};
end
end
%Look in bus2
fieldsBus2=fieldnames(bus2);
for u=1:length(fieldsBus2)
if(bus2.fieldsBus2{u}==matrix{i,1}) %Field names are stores in first column of matrix
%If bus field name matchs with field name stored in matrix, select that field name value as output
output=bus1.fielsBus2{u};
end
end
end
The thing is that if bus 1 and bus2 are small, this is, they have less than 30 elements, this code works well. But when I use large buses, this is for example, 290 elements, Simulink never finnish to compile the model. Do you have any insight about this problem? Thank you for your help.
I would expect that compilation time does not differ for 30 or 300 elements. I do not know why Simulink is not able to process this.

Réponse acceptée

Karan Singh
Karan Singh le 17 Juin 2024
Hi Jaime,
The long compilation times you're experiencing in Simulink with large buses are likely due to the inefficiencies in the nested loop structure of your MATLAB function, particularly when it processes large datasets. With each additional element in your buses, the computational complexity escalates exponentially, as does the number of iterations required, significantly extending compilation time.
To address this, consider the following optimized function. It leverages dynamic field names and minimizes the need for nested loops, potentially reducing compilation times:
function [output] = selector(bus1, bus2, inputAddress)
output = 0; % Default output initialization
% Assuming 'matrix' is accessible (either global or passed as a parameter)
% and 'numberOfMatrixRows' is defined or substituted with size(matrix, 1)
for i = 1:size(matrix, 1)
% Find the address
if inputAddress == matrix{i, 2} % Address is in the second column
fieldName = matrix{i, 1}; % Field name is in the first column
% Check and retrieve from bus1
if isfield(bus1, fieldName)
output = bus1.(fieldName);
break; % Exit once the field is found
end
% Check and retrieve from bus2
if isfield(bus2, fieldName)
output = bus2.(fieldName);
break; % Exit once the field is found
end
end
end
end
This version employs `isfield` to verify the existence of a field within a bus, then accesses it directly using dynamic field names (`bus1.(fieldName)`), eliminating the need for a secondary loop to identify a match. Importantly, it breaks from the loop upon finding the matching field and setting the output, thereby avoiding redundant iterations.
For further clarification, here's some documentation on `isfield`: https://www.mathworks.com/help/matlab/ref/isfield.html
Thanks,
Karan

Plus de réponses (0)

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by