Dynamically adding callback functionalities to uimenu items

6 visualizzazioni (ultimi 30 giorni)
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 Commenti
Shreedhar Savant Todkar
Shreedhar Savant Todkar il 17 Mag 2021
Modificato: Shreedhar Savant Todkar il 17 Mag 2021
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 il 17 Mag 2021
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.

Accedi per commentare.

Risposta accettata

Geoff Hayes
Geoff Hayes il 17 Mag 2021
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.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by