error when using matlab simulink block "level 2 Matlab S function''

12 views (last 30 days)
  • i want to read some data from a specific memory location using matlab simulink , so i create a simulink file as it shown *
  • inside level2matlab S-function block , the file called data_read.m is included ,in this file i want to call only one function called answer *
function read_data(block) %MSFUNTMPL_BASIC A Template for a Level-2 MATLAB S-Function % The MATLAB S-function is written as a MATLAB function with the % same name as the S-function. Replace 'msfuntmpl_basic' with the % name of your S-function. % % It should be noted that the MATLAB S-function is very similar % to Level-2 C-Mex S-functions. You should be able to get more % information for each of the block methods by referring to the % documentation for C-Mex S-functions. % % Copyright 2003-2010 The MathWorks, Inc.
%% %% The setup method is used to set up the basic attributes of the %% S-function such as ports, parameters, etc. Do not add any other %% calls to the main body of the function. %% setup(block);
%endfunction
%% Function: setup =================================================== %% Abstract: %% Set up the basic characteristics of the S-function block such as: %% - Input ports %% - Output ports %% - Dialog parameters %% - Options %% %% Required : Yes %% C-Mex counterpart: mdlInitializeSizes %% function setup(block)
% Register number of ports block.NumInputPorts = 1; block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic block.SetPreCompInpPortInfoToDynamic; block.SetPreCompOutPortInfoToDynamic;
% Override input port properties block.InputPort(1).Dimensions = 1; block.InputPort(1).DatatypeID = 0; % double block.InputPort(1).Complexity = 'Real'; block.InputPort(1).DirectFeedthrough = true;
% Override output port properties block.OutputPort(1).Dimensions = [5 1]; block.OutputPort(1).DatatypeID = 0; % double block.OutputPort(1).Complexity = 'Real';
% Register parameters block.NumDialogPrms = 0;
% Register sample times % [0 offset] : Continuous sample time % [positive_num offset] : Discrete sample time % % [-1, 0] : Inherited sample time % [-2, 0] : Variable sample time % block.SampleTimes = [0 0]; block.SampleTimes = [0 0]; % Specify the block simStateCompliance. The allowed values are: % 'UnknownSimState', < The default setting; warn and assume DefaultSimState % 'DefaultSimState', < Same sim state as a built-in block % 'HasNoSimState', < No sim state % 'CustomSimState', < Has GetSimState and SetSimState methods % 'DisallowSimState' < Error out when saving or restoring the model sim state block.SimStateCompliance = 'DefaultSimState';
%% ----------------------------------------------------------------- %% The MATLAB S-function uses an internal registry for all %% block methods. You should register all relevant methods %% (optional and required) as illustrated below. You may choose %% any suitable name for the methods and implement these methods %% as local functions within the same file. See comments %% provided for each function for more information. %% -----------------------------------------------------------------
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup); block.RegBlockMethod('InitializeConditions', @InitializeConditions); block.RegBlockMethod('Start', @Start); block.RegBlockMethod('Outputs', @Outputs); % Required block.RegBlockMethod('Update', @Update); block.RegBlockMethod('Derivatives', @Derivatives); block.RegBlockMethod('Terminate', @Terminate); % Required
%end setup
%% %% PostPropagationSetup: %% Functionality : Setup work areas and state variables. Can %% also register run-time methods here %% Required : No %% C-Mex counterpart: mdlSetWorkWidths %% function DoPostPropSetup(block) block.NumDworks = 1;
block.Dwork(1).Name = 'x1';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
%% %% InitializeConditions: %% Functionality : Called at the start of simulation and if it is %% present in an enabled subsystem configured to reset %% states, it will be called when the enabled subsystem %% restarts execution to reset the states. %% Required : No %% C-MEX counterpart: mdlInitializeConditions %% function InitializeConditions(block)
%end InitializeConditions
%% %% Start: %% Functionality : Called once at start of model execution. If you %% have states that should be initialized once, this %% is the place to do it. %% Required : No %% C-MEX counterpart: mdlStart %% function Start(block)
block.Dwork(1).Data = 0;
%endfunction
%% %% Outputs: %% Functionality : Called to generate block outputs in %% simulation step %% Required : Yes %% C-MEX counterpart: mdlOutputs %% function Outputs(block)
j=answer; block.OutputPort(1).Data =j ;
%end Outputs
%% %% Update: %% Functionality : Called to update discrete states %% during simulation step %% Required : No %% C-MEX counterpart: mdlUpdate %% function Update(block)
block.Dwork(1).Data = block.InputPort(1).Data;
%end Update
%% %% Derivatives: %% Functionality : Called to update derivatives of %% continuous states during simulation step %% Required : No %% C-MEX counterpart: mdlDerivatives %% function Derivatives(block)
%end Derivatives
%% %% Terminate: %% Functionality : Called at the end of simulation for cleanup %% Required : Yes %% C-MEX counterpart: mdlTerminate %% function Terminate(block)
%end Terminate
  • the answer function code * function j=answer% this function is made to transfer matrix j data j with dimensions 5*1% from one matlab session to another session by using memory mapping% function built in matlab
%this function reads the data from the memory and shows it as an output %firstly run 'send' function to write the data into the memory then run %'answer' function to read these data
%Create a Full File Name %myfolder\mysubfolder\myfile.m filename = fullfile(tempdir, 'talk_answer.dat');
% Create the communications file if it is not already there. if ~exist(filename, 'file') [f, msg] = fopen(filename, 'wb'); if f ~= -1 fwrite(f, zeros(1,256), 'double'); fclose(f); else error('MATLAB:demo:answer:cannotOpenFile', ... 'Cannot open file "%s": %s.', filename, msg); end end
% Memory map the file. m = memmapfile(filename, 'Writable', true, 'Format', 'double'); %read data from the file and j=m.data(1:5);
first i want to build this model then i run it when i press in build model this error appears
the error is "The corresponding 'read_data.tlc' file for the MATLAB S-function 'read_data' in block 'untitled/Level-2 MATLAB S-Function' must be located in the current working directory, the MATLAB S-function directory 'F:\thesis\send and recieve data', or the directory 'F:\thesis\send and recieve data\tlc_c'"
but i don`t know how i solve this problem because i am still a beginner in simulink please help me

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!