필터 지우기
필터 지우기

Any way get the filename into my print-command?

조회 수: 18 (최근 30일)
Anders Bennedsgaard
Anders Bennedsgaard 2015년 10월 3일
댓글: Star Strider 2015년 10월 3일
I'm trying to print out a plot of some of my data, and i'm trying to have the name of the filename of my data, as the name of the picture i get of it. Is there any way to automatically(and maybe a little easy) do this? Short version of my script:
Data = importdata('FILENAME.txt');
t = Data.data(:,1);
dist = Data.data(:,2);
figure; plot(t,dist)
print(FILENAME,-dpng) % Trying to dynamically get the filename out, instead of having to manually change this
I'm very new to Matlab, so go easy on me ;)

채택된 답변

Star Strider
Star Strider 2015년 10월 3일
If I understand your code correctly, I would use the file prefix, not the entire file name, to save the figure image, otherwise that risks problems. Also, unless FILENAME is a string, you have to put quotes around it as well. (I don’t use the print function often, so I’m relying on the documentation for guidance.) The fileparts function can be helpful in separating parts of the file name, making this easier.
  댓글 수: 2
Anders Bennedsgaard
Anders Bennedsgaard 2015년 10월 3일
I'm not much into the whole string-part yet. As i mentioned, i'm new to Matlab.. But fileparts worked out for me, so thanks ! :)
Star Strider
Star Strider 2015년 10월 3일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2015년 10월 3일
As you've written it, you're reading from 'FILENAME' as a fixed text string. You have all sorts of other options from having a list of filenames automatically generated by some sort of logic (often using a serial count) or reading them from a separate text file or by using a wild-card matching pattern in dir and then iterating over the resulting array d().name or letting the user select interactively with uigetfile which may be one or multiple files returned, your option.
After that, use whatever variable you choose for the file name and simply
title(filename)
will put that in the plot label.
[fn,path]=uigetfile('*.dat','Select file to process'); % select a file
Data=importdata(fullfile(path,fn)); % read it
...
title(['Experiment ' fn]) % put filename used on plot title
print(fn,'-dpng') % print to the filename w/ default extension
NB: must use string arguments in print with function form.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by