- 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.
Loading Multiple Images From Simulink User-Defined Script
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Rupesh
on 21 May 2024
Edited: Rupesh
on 21 May 2024
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.
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!