Dynamically adding callback functionalities to uimenu items

6 次查看(过去 30 天)
EDIT:
Following the suggestions to my question here, I was able to create a temporary file that stores the recently accessed files (here is the code):
function myRecentlyAccessedFiles(app, Fullfilename)
[~, name, ext] = fileparts(Fullfilename);
fichier = [name ext]; % creates the filename with extension
tempfile = 'temp.xlsx';
if ~exist(tempfile, 'file')
uimenu(app.OpenrecentlyaccessedfileMenu, 'Text', '< No files >');
writecell({''}, tempfile);
return;
else
writecell({fichier Fullfilename}, tempfile, 'WriteMode', 'append');
data = readcell(tempfile, 'FileType', 'SpreadSheet');
[C,ia,~] = unique(data(:,2), 'stable');
if numel(ia) > 5
maxLimit = 5;
else
maxLimit = numel(ia);
end
for i = 1 : maxLimit
tmp = uimenu(app.OpenrecentlyaccessedfileMenu, 'Text', char(C(i)), 'MenuSelectedFcn', @MenuSelected);
end
end
function MenuSelected(src,event)
% Execute this for each file
end
end
By doing so, after each iteration, the "Open recently accessed file" menu gave me the proper list (screensot below)
I need to open the file that is selected from the sub-menu of the "Open recently accessed file".
However, when I execute this, irrespective of the file chosen, the code always executes the last file in the list.
Am I doing something wrong here?
  3 个评论
Shreedhar Savant Todkar
编辑:Shreedhar Savant Todkar 2021-5-17
Hi Geoff,
Even if I simply try to print 'tmp.Text' in the MenuSelected function, it always prints the last filename from the list.
This is the menu selected function:
function MenuSelected(src,event)
setappdata(0, 'accessByMainWindow', 'False');
tmp.Text %Checking if the selected file is same as the file that will be processed.
setappdata(0, 'recentFile', tmp.Text);
BrowseWindow_optimized_App(); % <-- This function simply loads my file
% This function uses the 'tmp.Text' value (that contains the
% selected filename and uses it for further processing.
end
PS: I understand that 'setappdata' and 'getappdata' are not recommended, but in this case I just want it to work , I will opitmize it once I get this going.
Rik
Rik 2021-5-17
You can replace getappdata and setappdata with a hidden property of your app. All Matlab functions will have access to the groot object, so storing it there is a bad idea in general.
Wouldn't it be the best solution to store these names in a cell vector? By using a property the callback using the file can easily add the current file to the list, removing the oldest if the number of elements exceeds the max count or reordering if the file is already in the list.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2021-5-17
Shreedhar - in your loop, the variable tmp is always being overwritten
for i = 1 : maxLimit
tmp = uimenu(app.OpenrecentlyaccessedfileMenu, 'Text', char(C(i)), 'MenuSelectedFcn', @MenuSelected);
end
so this local variable will always refer to the last created menu item. I haven't tested this, but since your callback signature is
function MenuSelected(src,event)
then the src input parameter should correspond to the menu item object that was selected. In that case, you might be able to change your code to
function MenuSelected(src,event)
setappdata(0, 'accessByMainWindow', 'False');
setappdata(0, 'recentFile', src.Text); % <---- use src if possible
BrowseWindow_optimized_App(); % <-- This function simply loads my file
end
Again, I haven't tested this but it might* work.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by