Function syntax vs command syntax, uigetfile and load
显示 更早的评论
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 个评论
Steven Lord
2015-9-15
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
2015-9-15
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
2015-9-15
编辑:Guillaume
2015-9-15
采纳的回答
更多回答(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));
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!