Function syntax vs command syntax, uigetfile and load
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
Steven Lord
on 15 Sep 2015
What is the nature of the problem you're seeing when you run that code? Does it throw an error or issue a warning (and if so, what is the FULL EXACT text of the error or warning?) Does it give you a different result than you expected (and if so, what did you expect and what did you receive?)
If you're confused by the fact that there is no variable named x in your workspace when the function is finished executing, realize that functions operate in their own workspaces not in the base workspace.
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
on 15 Sep 2015
Edited: Guillaume
on 15 Sep 2015
Accepted Answer
More Answers (1)
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));
Categories
Find more on Entering Commands 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!