Clear Filters
Clear Filters

Refering to a imported filename in a plot legend

7 views (last 30 days)
I have make a script where I import 3 .dat files in a function. like "function myscript('file1.dat','file2.dat','file3.dat')". In the end of my script I make a plot of all of them where I would like to label these filenames (without the .dat). Right now, I how to manually write legend(file1,file2,file3).
  3 Comments
Alexander Kjærsgaard
Alexander Kjærsgaard on 15 Nov 2016
Now it is my turn to ask what do you mean? This is how I have coded it and it works this way (which is what I define as valid). What is wrong with the way I write it?
Jan
Jan on 15 Nov 2016
Edited: Jan on 15 Nov 2016
You cannot insert values in the defintion of the headline of a Matlab function. This is not valid Matlab syntax. Therefore we have to guess, what it should do. Perhaps you mean:
function myscript(file1, file2, file3)
...
and call this from e.g. the command line like:
myscript('file1.dat','file2.dat','file3.dat')
(without "function"). Or is the number of files variable?
Please read the "Getting Started" chapters of the documentation to see how to define a function. Without knowing the absolute basics, a discussion in the forum is very time-consuming for you - and for the readers.

Sign in to comment.

Answers (1)

Adam
Adam on 9 Nov 2016
doc fileparts
doc sprintf
  2 Comments
Alexander Kjærsgaard
Alexander Kjærsgaard on 15 Nov 2016
Hi, I don't understand how this helps me, can you explain in more depth?
Adam
Adam on 15 Nov 2016
Edited: Adam on 15 Nov 2016
fileparts helps you extract just the filename part of a file path, eliminating the directory and extension.
sprintf helps you put together a string from components, such as a filename and a number that can be dynamically rather than statically included and can be used in a legend.

Sign in to comment.

Categories

Find more on Data Import and Analysis 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!