フィルターのクリア

readmatrix error handling in Simulink Matlab function

2 ビュー (過去 30 日間)
Damien Pecher
Damien Pecher 2023 年 8 月 17 日
コメント済み: Paul 2023 年 8 月 18 日
I'm using the 'readmatrix' function inside a matlab function block in Simulink to read an Excel file that is often updated by an external software.
Simulink is sometimes crashing because it doesn't has access to the Excel file but I want Simulink to ignore this error and retry to read the file in the next simulation step.
I tried using a try/catch statement but this seems not to be available in Simulink.
All it needs to do is to try to access the file again instead of throwing an error and breaking the simulation, anyone who can help witrh this?
Thanks!
  2 件のコメント
dpb
dpb 2023 年 8 月 17 日
Where did you put the try...catch block and what does your .s function return if it can't read the file this step; maybe that's the problem that Simulink can't handle an empty return (just guessing, we can't see your simulation nor your code from here).
Damien Pecher
Damien Pecher 2023 年 8 月 18 日
I'm not using a .s function. I'm using the matlab function block in Simulink. I don't know how to use .s functions.
The error Simulink is returning is the following: Unable to open file 'Y:\data\EMS_Input.xlsx' as a workbook. Check that the file exists, read access is available, and the file is a valid spreadsheet file.
The file definately exists. The only thing is that I have an external application that overwrites the file every 5min and I'm guessing the error occurs when the application is writing the file and Simulink is trying to read the file at the exact same time...

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

採用された回答

Paul
Paul 2023 年 8 月 17 日
Try using exist:
function y = fcn(u)
coder.extrinsic('exist');
fileexists = 100.0; % dummy assignment to specify fileexists type for code generator
fileexists = exist('fu.xlsx','file');
if fileexists == 2
y = u;
else
y = -u;
end
end
  2 件のコメント
Damien Pecher
Damien Pecher 2023 年 8 月 18 日
Hi Paul,
Thanks for the input.
The file definately exists as I commented above. I think the problem is that I have an application that writes the file ans Simulink trying to read it at the exact same time.
I then want Simulink just to retry and not stopping the simulation as it is doing now.
Paul
Paul 2023 年 8 月 18 日
Assuming it's already been demonstrated that you can read the Excel file into the Simulink MatlabFunction block and that the error only shows up intermittently ....
Create an m-function file that does whatever is needed to read the Excel file and return information from the Excel file as output arguments. Inside this function use the try/catch to deal with the case when the readmatrix fails. Fucntion output arguments will still need to be returned in this case. With this function being somewhere on your MatlabPath, call it from the Simulink MatlabFunction block; make sure to declare it using the coder.extrinsic directive.
Something like this (untested):
function y = fcn(u) % this is the Matlab Function block
coder.extrinsic('readfile');
[a,b,c] = readfile;
end
function [a,b,c] = readfile % m-function on the Matlabpath
try
A = readmatrix('file.xlsx');
a = A(1);
b = A(2);
c = A(3);
valid = 1;
catch ME
a = 0;
b = 0;
c = 0;
valid = 0;
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by