Implementation of simulink model using a .m script

111 ビュー (過去 30 日間)
Divyashree
Divyashree 2024 年 10 月 29 日 7:04
コメント済み: Umar 2024 年 11 月 7 日 2:22
I have a .m scripts. It takes the excel sheets as input, performs analysis and output is again excel sheets. Similarly to .m scripts, how to implement simulink model?
split_MM.m is called in Processed_NVM_Fault_History.m
  1 件のコメント
praguna manvi
praguna manvi 2024 年 10 月 29 日 10:27
You can use "MATLAB Function" blocks after adding necessary blocks in the simulink model. Refer to this link :

サインインしてコメントする。

採用された回答

Aravind
Aravind 2024 年 10 月 29 日 11:26
It seems you're looking to process data from Excel sheets using Simulink and then save the results back into Excel.
To achieve this, you would need to first read the excel file in Simulink, process it record by record and then store the output into a modified excel file.
Reading an Excel File in Simulink:
To read data from an Excel file in Simulink, you can use the "From Spreadsheet" block. This block allows you to import data from Excel spreadsheets as a signal into your Simulink model. More details can be found at the following documentation link: https://www.mathworks.com/help/releases/R2019b/simulink/slref/fromspreadsheet.html.
Processing the Data in Simulink:
You can process the data read from the Excel file by creating a Simulink model that replicates the analysis performed in you “.M” script. This could involve using blocks for mathematical operations, logic, filtering, or any other processing required. You could use blocks like Math Operations, Signal Processing, and Logic and Bit Operations to build the analysis logic.
Additionally, you can also consider using the “MATLAB Function” blocks for complex calculations or custom scripts.
Storing the Modified Data to Another Excel File:
To save the processed data back into an Excel file, use the "To Workspace" block to write the signal data to a variable in the MATLAB workspace. Then, you can use the "writetable" function in MATLAB to export this data to an Excel file. More information about the "To Workspace" block can be found here: https://www.mathworks.com/help/releases/R2019b/simulink/slref/toworkspace.html.
Here are some additional resources to help you with this process:
  1. The “MATLAB Function” block: https://www.mathworks.com/help/releases/R2019b/simulink/ug/what-is-a-matlab-function-block.html
  2. Math Operations in Simulink: https://www.mathworks.com/help/releases/R2019b/simulink/math-operations.html
  3. Logic and Bit Operations in Simulink: https://www.mathworks.com/help/releases/R2019b/simulink/logic-and-bit-operations.html
  4. “writetable” function: https://www.mathworks.com/help/releases/R2019b/matlab/ref/writetable.html
  5. An example on using the “To Workspace” block: https://www.mathworks.com/help/releases/R2019b/simulink/slref/output-simulation-data-with-blocks.html
Hope this helps!

その他の回答 (1 件)

Umar
Umar 2024 年 10 月 29 日 16:18

Hi @Divyashree ,

After going through your comments, in order to read data from an Excel file within your Simulink model, you can utilize the From Spreadsheet block.

fromSpreadsheet

This block allows you to specify an Excel file from which it will read data. Here is how to configure it: Drag the From Spreadsheet block into your model. Set the File Name parameter to the path of your Excel file (e.g., myData.xlsx). Specify the Sheet Name if necessary. Ensure that your data is structured correctly: the first column should contain time data, and subsequent columns should contain signal values. If you have existing MATLAB scripts (like split_MM.m) as you mentioned in your comments, you can create a MATLAB Function block that calls these scripts: Create a new MATLAB Function block in your Simulink model. Open the MATLAB Function Block Editor by double-clicking the block. Define your function to call split_MM.m. For example:

     function output = processData(input)
         output = split_MM(input);
     end

In this case, ensure that split_MM.m is accessible in your MATLAB path. After processing the data, if you want to write results back to an Excel sheet, use the To Workspace block

toworkspace

or another MATLAB script that utilizes the writetable or xlswrite functions: Create a new MATLAB script that takes your processed output and writes it back to an Excel file:

     function writeOutputToExcel(data)
         writetable(data, 'outputData.xlsx');
     end

In your Simulink model: Connect the output of your From Spreadsheet block to the input of your MATLAB Function block. Connect the output of your MATLAB Function block to a To Workspace or another appropriate block for writing back to an Excel file. When you run the simulation:The From Spreadsheet block will read input data from your specified Excel file. Your custom processing logic in split_MM.m will execute via the MATLAB Function block. Finally, processed results can be written back to another Excel file. Ensure all paths are correctly set so that both reading from and writing to Excel files works seamlessly. The structure of your Excel files is crucial; verify that they conform to expectations (e.g., time-stamped format).

By following these steps, you should be able to effectively integrate your existing .m scripts with Simulink while leveraging Excel as both input and output sources for data processing.

Hope this helps.

  11 件のコメント
Divyashree
Divyashree 2024 年 11 月 6 日 17:28
Can you please let me know, which data type is better to use? Double or string? Because it contains both alpha numeric characters. I've attached the snaps below.
Umar
Umar 2024 年 11 月 7 日 2:22

Hi @Divyashree ,

Please see my response to your comments below.

Size computation for 'processed_data' failed. Unknown var 'N' used in size definition.Size computation for 'processed_data' failed. Unknown var 'N' used in size definition. In the from spreadsheet model there is no changes, the file name, sheet name, range and the output datatype is same as I've mentioned before and connected all the 38 signals of the spreadsheet to a bus creator. Also in the workspace model there are no changes. I made few modifications in the simulink model. I've implemented a .m script for the bus creator and the block parameter I've mentioned it as outputdatatype: Bus:MyBusType, mode: bus object, MyBusType. The .m script I've mentioned below. The reason I've created this object code is

defineBusObject.m
% Create the bus object
busElements(38) = Simulink.BusElement; 
% Preallocation for 38 elements
% Define each element in the bus
for i = 1:38
  busElements(i).Name = ['Signal' num2str(i)];
  busElements(i).Dimensions = 1;
  busElements(i).DataType = 'double';
end
% Assign to MyBusType
MyBusType = Simulink.Bus;
MyBusType.Elements = busElements;
% Save MyBusType in the base workspace
assignin('base', 'MyBusType', MyBusType);

After running the above object code, MyBusType will be in the base workspace.Before running the model, I executed the above script and made few modifications in the simulink model properties, that is Callback -> either in pre-loadfcn/InitFcn -> defineBusObject. After making this changes, I executed the model and I'm facing the above error.

Comments: Ensure that the variable N is defined in the MATLAB base workspace before running the Simulink model. You can do this by executing the following command in the MATLAB Command Window:

   N = 38; % or whatever value is appropriate for your model

Your script for creating the bus object appears to be correct. However, ensure that the bus object is indeed being created and assigned to the base workspace before the model runs. You can verify this by checking the base workspace:

evalin('base', 'whos') 
% This will list all variables in the base workspace

You mentioned using the InitFcn or PreLoadFcn callbacks to run the defineBusObject.m script. Ensure that these callbacks are correctly set up in the model properties. You can check this by right-clicking on the model background, selecting "Model Properties," and navigating to the "Callbacks" tab. Ensure that the script is correctly referenced:

defineBusObject

I've attached the snapshot of the model and the port and data manager. Also let me know should I need to make any changes in the ports and data manager of the matlab function. And also, Why am I geeting 1,1,1,1,1,1.... between the spreadsheet and buscreator model and '?' between bus creator and matlab function and workspace model.

Comments: If you are seeing unexpected values (like 1,1,1,1,1,1...) between the spreadsheet and the bus creator, it may indicate that the data is being interpreted incorrectly. Ensure that the data types in the Ports and Data Manager are consistent with what you expect. You can access the Ports and Data Manager by right-clicking on the bus creator block and selecting "Block Parameters." Check the data types and dimensions of the signals connected to the bus creator. To further debug the model, consider using the Simulink Debugger. You can set breakpoints in your model to inspect the values of variables at different points in the execution. This can help you identify where the unexpected values are being introduced. For reference, please click the link below.

https://www.mathworks.com/help/simulink/simulation-stepper.html

Regarding your issue with handling ‘?’ Between blocks, the ? symbol typically indicates a mismatch in data types or dimensions between connected blocks. Ensure that the output data type of the bus creator matches the expected input data type of the subsequent MATLAB function block. You can do this by checking the block parameters and ensuring that the data types are compatible. Can you please let me know, which data type is better to use? Double or string? Because it contains both alpha numeric characters. I've attached the snaps below.

Comments: Regarding your question about data types, if your data contains both alphanumeric characters, it is advisable to use a string data type. In MATLAB, you can use the string or char data types depending on your requirements. For example:

   myData = "Sample123"; % Using string data type

Hope this helps.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEvent Functions についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by