## How can I solve the variable size matrix problem in my embedded Matlab function in Simulink

### Ralf Sch (view profile)

on 3 Jul 2013

Hello, I am working since a month with Simulink and have to simulate a vehicle. Currently I have some problems with my gear switch function which is written is a Matlab function and I try to embed in the Simulink model. I tried several ways but still I did not get any solution, perhaps somebody of you have a good idea? Here is my code for the gear selection:

% code
function [Ttot, gearind] = ...
gearselection(Ttotal, wg, Tg, Tmmax, Temax, Te0, gearnumvec)
Tg=Tg(1,1);
gearind=1;
Ttot=0;
coder.extrinsic('find');
if Tg>=0 && wg>0
Tmax = Temax+ Tmmax;
udiff=(Tmax-Ttotal)./Ttotal;
Tmax(udiff<0.1)=0;
Tmax(Temax==0)=0;
Tmax(Tmmax==0)=0;
gearind = find(Tmax, 1, 'last');
Ttot=Ttotal(gearind)-Te0(gearind);
if isempty(gearind)
Tmax = Temax+ Tmmax;
Tmax(udiff<0.1)=0;
gearind = find(Tmax, 1, 'last');
Ttot=Ttotal(gearind)-Te0(gearind);
end
if isempty(gearind)
Tmax = Temax+ Tmmax;
Tdiff=Ttotal-Tmax;
Tdiff(Tmax==0)=10000;
[~, gearind]= min(Tdiff);
Ttot=Tmax(gearind)-Te0(gearind)-0.1.*Tmax(gearind);
end
elseif Tg<0 && wg>0
Tmmin=-Tmmax;
udiff=(Tmmin-Ttotal)./Ttotal;
Tmmin(udiff<0.1)=0;
gearind = find(Tmmin, 1, 'last');
Ttot=Ttotal(gearind);
if isempty(gearind)
Tmmin=-Tmmax;
Tdiff=Ttotal-Tmmin;
Tdiff(Tmmin==0)=-1000;
[~,gearind]=max(Tdiff);
Ttot=Tmmin(gearind);
end
else
[~, gearind] = min(gearnumvec);
Ttot=0;
end
end
end

As you see the function shall compute the the needed torque which is needed to drive the car and the index of the gear vector. The problem which I get is, that I get always the message: Size mismatch for MATLAB expression 'find'. Expected = 1x1 Actual = 0x1 Block Gearbox_backward/gear_selection (#25) While executing: none; while running the model. I tried already to change the size of the variable in the Port and Data Manager, but that is also not working. Another possibility is to change this code into a Simulink model, but this was not possible for me, I had always problems with the find function of Simulink. Would perhaps State-Flow a possibility? I haven't worked with State-flow so much either.

I am looking out for your replies and thank you in advance.