필터 지우기
필터 지우기

How to get output of eventhandler function to main function

조회 수: 1 (최근 30일)
S.J.
S.J. 2021년 6월 16일
답변: Vatsal 2024년 3월 13일
I have written a code that is linked to Outlook via actxserver in order to get data from the latest mail i got. I wish to process the event function and then pass the output of it back to the main function, where i want to further process the data (parse it). But i get this error:
Error using registerevent
Too many output arguments.
My main function is:
outlook = actxserver('Outlook.Application');
[subject, body] = registerevent(outlook,{'NewMail', 'MailEvent'})
% some more code where i need subjct and body data
%...
and the MailEvent function is:
function [subject, body] = MailEvent(varargin)
outlook = varargin{1};
% Retrieving last email
mapi=outlook.GetNamespace('mapi');
INBOX=mapi.GetDefaultFolder(6);
count = INBOX.Items.Count; %index of the most recent email.
firstemail=INBOX.Items.Item(count); %imports the most recent email
INBOX.Items.Item(count).Unread=0;
subject = firstemail.get('Subject');
body = firstemail.get('Body');
end
If i leave the output away and use it this way it works, but the subject and body data is "captured" in the Mailevent workspace
outlook = actxserver('Outlook.Application');
registerevent(outlook,{'NewMail', 'MailEvent'})
How can i solve this ?

답변 (1개)

Vatsal
Vatsal 2024년 3월 13일
Hi,
The issue you are facing is due to the fact that the "registerevent" function in MATLAB does not support output arguments. It is designed to register a callback function (in given case, MailEvent) that gets executed when a specific event occurs. However, it does not return any values from the callback function.
A potential solution involves the use of global variables to transfer data from the MailEvent function to the main function.
The code could be adjusted in the following manner:
% Declare global variables
global subject body
% Your main function
outlook = actxserver('Outlook.Application');
registerevent(outlook,{'NewMail', 'MailEvent'})
% Now subject and body can be accessed here
% some more code where you need subject and body data
%...
% MailEvent function
function MailEvent(varargin)
% Make sure to declare the variables as global in this function as well
global subject body
outlook = varargin{1};
% Retrieving last email
mapi=outlook.GetNamespace('mapi');
INBOX=mapi.GetDefaultFolder(6);
count = INBOX.Items.Count; %index of the most recent email.
firstemail=INBOX.Items.Item(count); %imports the most recent email
INBOX.Items.Item(count).Unread=0;
subject = firstemail.get('Subject');
body = firstemail.get('Body');
end
I hope this helps!

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by