Imread() Error in Simulink Function Block
Show older comments
In Simulink, I have a user-defined function block whose main purpose is to read an image from a URL and output to a video viewer block. However, the imread(url) throws an error as follows:
Caused by: Cannot open file "https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/mex/GEOCOLOR/1000x1000.jpg"
for reading. FILENAME must be the absolute or relative path to the file.
The same imread works fine in the MATLAB terminal. Below is the full code in the function block. The objective of the function is to get a satellite image from a URL and send it to a video viewer block to view.
The input is the value of a constant block and the output (y) should be an image.
function y = fcn(u)
switch u
case 0
URL = 'https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/gm/GEOCOLOR/1000x1000.jpg';
case 1
URL = 'https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/GEOCOLOR/1200x1200.jpg';
case 2
URL = 'https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/eus/GEOCOLOR/1000x1000.jpg';
end
myImage = imread(URL); %error happen here
y = myImage;
I'm unable to figure out what is going wrong or how to get my code working, I would appreciate any input and help.
Answers (1)
Manish
on 25 Nov 2024
Hello Prudvi Krishna,
I understand that you are encountering an error while using the ‘imread’ function in Simulink.
A practical workaround is to use the ‘websave’ function, which allows you to save content from a RESTful web service to a local file. After saving the image locally, you can then read it using ‘imread’.
Here is updated code:
function y = fcn(u)
coder.extrinsic('websave', 'imread', 'tempname', 'delete');
switch u
case 0
URL = 'https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/gm/GEOCOLOR/1000x1000.jpg';
case 1
URL = 'https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/se/GEOCOLOR/1200x1200.jpg';
case 2
URL = 'https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/eus/GEOCOLOR/1000x1000.jpg';
end
y = zeros(1200, 1200, 3, 'uint8'); % Adjust dimensions accordingly
tempFileName = [tempname, '.jpg'];
websave(tempFileName, URL);
myImage = imread(tempFileName);
delete(tempFileName);
y = myImage;
end
Here is the Simulink model:

Refer the links below for better understanding:
- https://www.mathworks.com/help/matlab/ref/imread.html
- https://www.mathworks.com/help/matlab/ref/websave.html
Hope this helps!
Categories
Find more on Image Acquisition in Simulink 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!