Appdesigner TextArea display warning message from function
古いコメントを表示
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
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

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:
- evalc: https://www.mathworks.com/help/releases/R2021a/matlab/ref/evalc.html
- strsplit: https://www.mathworks.com/help/releases/R2021a/matlab/ref/strsplit.html
I hope this helps!
カテゴリ
ヘルプ センター および File Exchange で Develop Apps Using App Designer についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!