0
votes

I want to generate an arbitrary linear signal from matlab function block in simulink. I have to use this block because then, I want to control when i generate the signal by a sequence in Stateflow. I try to put the out of function as a structure with a value field and another time as the following code:

`function y = sig (u) 

if u == 1 
t = ([0 10 20 30 40 50 60]); 
T = [(20 20 40 40 60 60 0]); 


S.time = [t ']; 
 S.signals (1) values ​​= [T '].; 
S.signals (1) = 1 dimensions.; 

else 
N = ([0 0 0 0 0 0 0]); 
S.signals (1) values ​​= [N '].; 
end 
y = S.signals (1). values 
end `

the idea is that u == 1 generates the signal, u == 0 generates a zero output.

I also try to put the output as an array of two columns (one time and another function value) with the following code:

function y = sig (u) 

if u == 1 
S = ([0, 0]); 
cant = input ('Number of points'); 
for n = Drange (1: cant) 
S (n, 1) = input ('time'); 
S (n, 2) = input ('temperature');  
end 
y = [S] 
else 
y = [0] 
end 
end

In both cases I can not generate the signal.

In the first case I get errors like:

This structure does not have a field 'signals'; new fields can not be added When structure has-been read or used

or

Error in port widths or dimensions. Output port 1 of 'tempstrcutsf2/MATLAB Function / u' is a one dimensional vector with 1 elements.

or

Undefined function or variable 'y'. The first assignment to a local variable Determines its class.

And in the second case:

Try and catch are not supported for code generation,

Errors occurred During parsing of MATLAB function 'MATLAB Function' (# 23)

Error in port widths or dimensions. Output port 1 of 'tempstrcutsf2/MATLAB Function / u' is a one dimensional vector with 1 elements.

I'll be very grateful for any contribution.

PD: sorry for my English xD

1
Your code looks poorly formatted, at least contains several syntax errors. Please review it.Yvon
Why not use a Signal Builder block or From Workspace block, inside an Enabled Subsystem?am304

1 Answers

0
votes

You have many mistakes in your code you have to read more about arrays and structs in matlab

  1. here S = ([0, 0]); you're declare an array with only two elements so the size will be static
  2. There is no function called Drange in mtlab except if it's yours

See this example with strcuts and how they are created for you function

function y = sig(u)
    if u == 1 
        %%getting fields size
        cant = input ('Number of points\r\n'); 

        %%create a structure of two fields 
        S = struct('time',zeros(1,cant),'temperature',zeros(1,cant));

        for n =1:cant 
           S.time(n) = input (strcat([strcat(['time' num2str(n)]) '= '])); 
            S.temperature(n) = input (strcat([strcat(['temperature'...
                num2str(n)]) '= ']));  
        end 
            %%assign output as cell of struct 
            y = {S} ;
        else 
            y = 0 ;
    end 
end

to get result form y just use

s = y{1};
s.time;
s.temperature;

to convert result into 2d array

2dArray = [y{1}.time;y{1}.temperature];

Work with arrays

function y = sig(u)
    if u == 1 
        cant = input ('Number of points\r\n');
        S = [];
        for n =1:cant
            S(1,n) = input (strcat([strcat(['time' num2str(n)]) '= ']));
            S(2,n) = input (strcat([strcat(['temperature'...
                num2str(n)]) '= ']));
        end
        y = S;
    else 
         y = 0 ;
    end 
end