- plot() is a built-in function so you should not name your own function as plot.
- It is better to have an input argument specify the folder so you can use it to apply to many folders.
- When load or save, it's always better to specify the full path of the file.
- When you try to get the file name, you need to get rid of the .txt extension.
- close() needs to specify the figure handle.
Function
1 次查看(过去 30 天)
显示 更早的评论
I'd like to be able to apply this function loads,plots and saves figure data from a text file, to every file in a directory, such that when I type FunctionName(some directory), the function forks...
this is my code:
function plot;
files = dir('*.txt');
for i=1:length(files)
data = load(files(i).name);
filename=[files(i).name];
plot(data);
saveas(h,filename,'fig');
close;
end
end
0 个评论
采纳的回答
Fangjun Jiang
2011-11-9
function MyPlot(PathStr)
files = dir(fullfile(PathStr,'*.txt'));
for i=1:length(files)
data = load(fullfile(PathStr,files(i).name));
filename=strrep(files(i).name,'.txt','');
f=figure;
plot(f,data);
saveas(h,fullfile(PathStr,[filename,'.fig']));
close(f);
end
更多回答(1 个)
Daniel Shub
2011-11-9
What problems are you having. the code looks pretty close. You don't define h, so I just replaced it with gcf (the current figure). Your function didn't take in the directory name like you need (or make use of the directory name).
function FunctionName(DirectoryName)
cd(DirectoryName)
files = dir('*.txt');
for i=1:length(files)
data = load(files(i).name);
filename=[files(i).name];
plot(data);
saveas(gcf,filename,'fig');
close;
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!