Clear Filters
Clear Filters

FMU Export with .csv as input

39 views (last 30 days)
Sharul Baggio
Sharul Baggio on 20 Aug 2024 at 12:56
Answered: Walter Roberson on 21 Aug 2024 at 17:37
Wanted to check if it will be possible to export as FMU standalone with the input being "From Spreadsheet".
I have tried creating and I was given an error:
"Error:Error encountered in block ResistanceModel_FMU/From Spreadsheet
Caused by:
From Spreadsheet block supports rapid simulation target and the following simulation modes: Normal, Accelerator, and Rapid Accelerator."
Is there an alternative route to doing this, because I plan to have the input for the user to upload their respective file when the FMU is set up.
  1 Comment
Umar
Umar on 20 Aug 2024 at 18:07
Edited: Umar on 20 Aug 2024 at 18:08

Hi Sharul Baggio,

The error message you received indicates that the "From Spreadsheet" block is not compatible with the FMU export process in certain configurations. To work around this limitation, consider using a different approach for input handling. One alternative is to utilize a MATLAB function block that reads the spreadsheet data at runtime. This block can be designed to accept user-uploaded files, allowing for dynamic input during simulation.

function data = readSpreadsheet(filePath)
    % Read data from the specified spreadsheet file
    data = readtable(filePath);
end

You can then call this function within your model to load the data dynamically. Hope, this method should help resolve your issue. Please let me know if you have any further questions.

Sign in to comment.

Answers (3)

Sharul Baggio
Sharul Baggio on 21 Aug 2024 at 8:43
Hi @Umar thanks for the suggestion.
I have tried the function you mentioned and was greeted with the following error message.
Error:Function 'readtable' not supported for code generation.
More information
Function 'MATLAB Function' (#91.106.125), line 3, column 12:
"readtable(filePath)"
Launch diagnostic report.
I have tried using readCSV and csvread, and was given the same error as well.
Image below is how I connected the function.
Addition info, I export to FMU with the following steps.
Save>export model to standalone FMU> click fmi 3.0, changed destination folder and click create.
Then the error above was given.
I have also tried swapping the input for speed (v) with a fixed step, to experiment exporting, but was given the
"Code Generation information file does not exist" error.
Thanks in advance for the major help!
  1 Comment
Umar
Umar on 21 Aug 2024 at 13:33

Hi @ Sharul Baggio,

That is frustrating. It sounds like these certain MATLAB functions which you tried including readtable, readCSV, and csvread, are not designed to be used in code that is generated for deployment or simulation in environments that do not support these functions. I just went through documentation

https://www.mathworks.com/help/matlab/ref/csvread.html

it mentions that csv not recommended, you have to use read matrix for R2019a and above. Here is documentation for read matrix

https://www.mathworks.com/help/matlab/ref/readmatrix.html

Also, this documentation below shows supported file format for export and import

https://www.mathworks.com/help/matlab/import_export/supported-file-formats-for-import-and-export.html

Also, instead of using unsupported functions for code generation, consider importing data directly through Simulink blocks designed for this purpose. Use blocks like From Spreadsheet or From File that can read data without relying on unsupported MATLAB functions. If you have data stored in CSV format, you can convert it into a format compatible with Simulink, such as using a Constant block to feed in predefined values or using MATLAB Function blocks with compatible coding practices. This is the last alternative to try, Set your model to use a fixed-step solver:

        set_param('your_model_name', 'Solver', 'FixedStepAuto')
 Use the exportToFMU function correctly by specifying necessary parameters:
        exportToFMU('your_model_name', 'FMIVersion', '3.0', 'FMUType', 'CS')

Make sure that any variable inputs or outputs are properly defined and that their data types are compatible with FMI standards.

If you continue to face issues, consider simplifying your model incrementally and testing exports until you isolate the source of errors.

Sign in to comment.


Rahul
Rahul on 21 Aug 2024 at 9:28
I understand that you are trying to use the "From Spreadsheet" block for your use-case in which the end user will be uploading their spreadsheet file to run the model while the FMU is set up.
As per my understanding the "From Spreadsheet" block does not support generating code that involves building ERT or GRT targets, or using SIL or PIL simulation modes. Hence in this case the FMU is unable to generate code as also mentioned in this MATLAB Answer: https://www.mathworks.com/matlabcentral/answers/2146699-how-can-i-export-roboflow-annotation-to-work-in-matlab?s_tid=srchtitle
A possible workaround could be using "From Workplace" block after saving the spreadsheet content into a workspace variable.
You can refer to the following documentations for reference:
Hope this helps! Thanks.

Walter Roberson
Walter Roberson on 21 Aug 2024 at 17:37
You have the fundamental problem that when you deploy to the FMU, the FMU does not have a filesystem to be able to potentially read from a spreadsheet.
The same problem affects most of the potential work-arounds.
Although I do not work in this area myself, I seem to recall that the workaround is to save the data to a .mat file before-hand, and load() the .mat file in the code. If I recall correctly, this will compile the .mat content right into the code. This has the disadvantage that changes to the spreadsheet or .mat file require recompiling.
As best I know, there is no work-around that allows "live" data to be read in on the FMU -- the FMU just doesn't have the right infrastructure to store data.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!