jonathan.charbonneau
jonathan.charbonneau

Reputation: 51

"From File" Simulink block called from Matlab .m file in loop

I am calling a Simulink simulation through a loop in Matlab. I am able to send many numerical parameters (initial conditions for integrators for example), but an error arises in my "From File" block.

I'm not quite sure how other people do it, but where I work we send it through a home-built function which takes a structure input. The fields of the structure are the variable names and the values of the fields are the variables themselves. for example:

pb = struct('preload',preload(pl_index),...
            'displacement',preload_displacement(pl_index),...
            'filename',fileList{m});

The 'preload' and 'displacement' variables evaluate fine, but the filename gives an error:

filename.mat

Does anyone know if you can pass the value of the variable filename (fileList{m}) in the 'From File' block and if so, how to do it. Thanks!

Upvotes: 0

Views: 478

Answers (1)

jonathan.charbonneau
jonathan.charbonneau

Reputation: 51

Answering my own question!

I used the Simulink "From Workspace" block instead of "From File".

f = load(fileList{m});
fnames = fieldnames(f);
% The files were arranged weird, so I have one field inside the structures... and they all had different names.
    switch fnames{1}
        case 'first'
            filedata = f.first;
        case 'second'
            filedata = f.second;
        case 'third'
            filedata = f.third;
        case 'fourth'
            filedata = f.fourth;
    end
    t = filedata(1,:);
    u = filedata(2,:);

loaded_file = timeseries(u,t);

And I pass loaded_file to my function.

Upvotes: 1

Related Questions