Appdesigner TextArea display warning message from function

5 次查看(过去 30 天)
How do I display warning messages created in a function, outside of the app. Like a feedback system if certain tasks succeed.
For example:
function [data]=somefunction(filename)
if ~isfile(filename)
warning('File not found')
end
data = readtable(filename);
end
This is called by a buttonpushedfunction
function OpenButtonPushed(app, event)
[app.data] = initvar();
app.TextArea.Value =[app.TextArea.Value];%, warning('')];
end
How do I connect the warning message to the value in the TextArea?
The warning message does show in the command window in Matlab.
I have tried adding the link in the warning message in the function, but this did not help.
Thank you in advance.

回答(1 个)

Abhipsa
Abhipsa 2025-2-17
You can use the “evalc” function in MATLAB to capture all output that would typically be displayed in the command window, including warnings, during the execution of a specified expression. By using this function, you can redirect these outputs to a string variable.
The captured output can then be processed by splitting into individual warning messages based on newlines. This approach effectively captures all warnings generated during the execution of a function.
Here is how the two pieces of code that you have provided can be connected to implement the feedback system:
% I am assuming that the required properties names have been created
%This is a callback function for button in app designer
function OpenButtonPushed (app, event)
% Initialize a string to store warning messages
warnStr = evalc(' somefunction(app, file.csv'');'); %replace ‘file.csv’ with your file name
% You can use a regular expression to clean up non-printable characters (optional step)
cleanWarnStr = regexprep(warnStr, '[^\x20-\x7E]', '');
cleanWarnStr = strtrim(cleanWarnStr); % Trim leading/trailing whitespace
% Split the cleaned string into separate messages
warningMessages = strsplit(cleanWarnStr, newline);
warningMessages = warningMessages(~cellfun('isempty', warningMessages)); % Remove empty lines
% Display all captured warnings in the TextArea
if ~isempty(warningMessages)
app.TextArea.Value = [app.TextArea.Value; warningMessages'];
else
app.TextArea.Value = [app.TextArea.Value; {'File loaded successfully.'}];
end
end
The output of the same in two cases:
1. File found
file found
2. File not found
NOTE: I wanted to display multiple warning messages, so I have added some dummy warnings in the code for the same.
You can use the below MATLAB documentations for further details:
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by