Loading Multiple Images From Simulink User-Defined Script

3 ビュー (過去 30 日間)
Jafar Noor
Jafar Noor 2021 年 4 月 30 日
編集済み: Rupesh 2024 年 5 月 21 日
Hello
I have been developing a Simulink model that attempts to load a series of JPG images from a local folder. To the best of my knowledge, Simulink does not offer a built-in block with such capability and that is why I am using a user-defined function. To construct the image titles, I am using a for-loop as following:
for i = 1:5
for j = 1:5
title = [int2str(i),'_','int2str(j)','.jpg'];
image = imread(title);
end
end
When using the above code, I get the dimensionality error in Simulink that suggest the imread function can't load an image with such naming construction:
Error message: Dimension 1 is fixed on the left-hand side but varies on the right ([369 x 440 x 3] ~= [:? x :? x :?]).
However, if I just write the title name in the script (e.g. title = ['1_1.jpg']) things work fine. My other problem is regarding converting this Simulink block into C codes. Even if I define the image title in the script (which works fine for Simulink), the coder will give the above error message.
I really appriciate any help and suggestion on these matters

回答 (1 件)

Rupesh
Rupesh 2024 年 5 月 21 日
編集済み: Rupesh 2024 年 5 月 21 日
Hi Jafar
I understand that you're facing issues in loading JPG images in Simulink using a User-Defined Function (UDF) block. After going through the code i think there are 2 possible cases in your code which caused problems. One of them is related to name-mismatch and another one is User-defined function block compatibility. Below are some possible workarounds to resolve the issue.
  • In the first case, the issue lies in how you concatenate the strings. Instead of concatenating the result of int2str(j) as a string, you're concatenating the string 'int2str(j)', which is getting extra quotes, and because of this “imread” issue arose.
  • In the second case when converting your Simulink model to C code using Simulink Coder, it's important to ensure that all MATLAB functions and operations used in your model are compatible with code generation. In a given scenario, the “imread” function might not be directly supported for code generation, or there might be restrictions on its use. One possible workaround is to pre-load all the images before generating code and then pass the loaded images as data inputs to your generated code.
You can follow below general outline for the mentioned workaround implementation.
% Preload images
Load all the images outside of the generated code using MATLAB script or function.
% Pass images to Simulink and access the results
simOut = sim('YourSimulinkModel', 'Externalnput', 'images');
results = simOut.YourOutputSignal;
In your Simulink model, you can use an input port to receive the images. Then, process them as needed within the model. For example, if you have an Image Processing block, you can use it to perform operations on the input images. You can refer to the below documentation for better understanding of C code generation from the Simulink model.
Hope it helps!
Thanks

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by