Function syntax vs command syntax, uigetfile and load

4 views (last 30 days)
Hi there!
I'm trying to code an interface that will look for a file, with uigetfile, then use this file name to load the data, in this case a 60000x3 matrix (but it could be different), then plotting those datas. I'm using this code:
[name path]=uigetfile('*.txt');
x=load(name);
plot(x);
The strange thing is this code is working on command line, but not if I use it in a function. In my function the code is:
menu=uimenu(f, 'Label', 'test');
load=uimenu(menu, 'Label', 'Load',...
'Callback', {@appel});
function[]=appel(varargin)
[name path]=uigetfile('*.txt');
x=load(name);
plot(x);
end
Any idea where the problem come from?
  3 Comments
Stephen23
Stephen23 on 15 Sep 2015
The title "Function syntax vs command syntax" is completely unrelated to the question: there is no example of command syntax anywhere in the question, the question uses only function syntax. These terms do not relate to if a function is called from another function, but refer to the syntax of how a function is called:
Pierre THOMAS
Pierre THOMAS on 15 Sep 2015
Edited: Guillaume on 15 Sep 2015
@Steven Lord, I re-tryed with the code: function retest
fh = figure('units','pixels',...
'position',[300 300 400 250],...
'menubar','none',...
'name','Test pop box',...
'numbertitle','off',...
'resize','off');
ax=axes('units', 'pixels',...
'position', [20 50 360 200]);
menu=uimenu(fh, 'Label', 'test');
load=uimenu(menu, 'Label', 'Load',...
'Callback', {@appel});
function[]=appel(varargin)
[name path]=uigetfile('*.txt');
x=load(name);
plot(x);
end
end
and the error msg is:
Index exceeds matrix dimensions.
Error in retest/appel (line 19)
x=load(name);
Error while evaluating uimenu Callback
I hope it will help.
As I said, using uigetfile and load is working in command line, this is why I put that in the title with the exact same syntax.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 15 Sep 2015
The error message kind of gives away what the problem is. It's telling you that the index in
x = load(name)
exceeds the dimension of the matrix it is applied to. There can only be one thing that matlab can interpret as an index on that line, the name variable, and more importantly, only one thing that matlab can interpret as an matrix being indexed and that is load. The only way that matlab would think that load is a matrix instead of the built-in load function is if you have created a variable with that name.
Sure enough, a few lines above you have
load = uimenu(menu, 'Label', 'Load',...
'Callback', {@appel});
Morale of the story, don't use built-in function names for your variables.
A good practice is to use variable names that actually explain what they contain in detail. Rather than name (name of what?) use datafilename (or configfilename, or ...), and rather than load, use uiloadbutton. The purpose of the variable is immediately more obvious, which always results in less debugging time.

More Answers (1)

Stephen23
Stephen23 on 15 Sep 2015
Edited: Stephen23 on 15 Sep 2015
The problem
Your variable names path and load and menu.
Look at these lines:
menu=uimenu(fh, 'Label', 'test');
load=uimenu(menu, 'Label', 'Load',...
and
[name path]=uigetfile('*.txt');
As you are discovering these are really bad names for variables, because path and load and menu are all important inbuilt functions, so you should never name your own variable with these names because this stops those functions from working. When you define variables with those names so you are telling MATLAB to ignore the functions!
The Solution
Rename those variables.
Note that you can use which to check which names are already being used.
And also you should use fullfile to create the full filename:
[fname,fpath] = uigetfile('*.txt');
x = load(fullfile(fpath,fname));

Community Treasure Hunt

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

Start Hunting!