Function syntax vs command syntax, uigetfile and load
4 views (last 30 days)
Show older comments
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
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:
Accepted Answer
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.
0 Comments
More Answers (1)
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.
[fname,fpath] = uigetfile('*.txt');
x = load(fullfile(fpath,fname));
0 Comments
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!