How to create a title in plot such that it has the same name as the .txt file name being imported?

Hello,
I have a 6x1 subplot, and it is missing a title. I want the title to be named based on the name of the .txt file I imported. The reason for this is because I plan on importing different file names with different data and I would like the plots to be titled accordingly, based on those file names. So for example, if I imported a .txt file called "TestCase1", I would like the title of the subplot to be "TestCase1.'
I understand that the function to have a plot title is title('text'), but I am not exactly sure how to call the name of the file I imported.
I'm not sure if this helps, but the way I'm currently importing files is using the following.
data = uiimport
Then a window pops up and asks to select a data input source. I click on "File", then select a .txt file to import. Also, after importing and looking at the data, it does not contain the name of the file imported.
I hope this makes sense, if not I can explain further. I am using 2015a btw. Any advice or suggestions would be appreciated. Thanks.

 Accepted Answer

Use uigetfile to select the file and then call uiimport with it
[file,filepath] = uigetfile()
S = uiimport(fullfile(filepath,file))
Then
plot(whatever)
title(file)

5 Comments

Thanks for your quick response Sean. It looks like your suggestion is working pretty well by using the following.
[file] = uigetfile({'*.*'})
S = uiimport(fullfile(file))
I included { '*.*' } in uigetfile to look at all files, and eliminated one of the outputs, filepath, because I did not need it. However, is there a way to get rid of the file type in the file name? Is that what the fullfile() is for? Right now, my title is "TestCase1.txt" instead of "TestCase1".
You can use fileparts to grab the name without the extension.
doc fileparts
Full file is to create the full file path including the path in case the user moved to a different directory with uigetfile
Hm, ok I see what you're saying. Thanks for your help Sean. However, there's one more issue regarding fileparts. I'm having trouble parsing out only the name of the file. I use the following.
[file] = uigetfile({'*.*'});
TestCase1 = uiimport(fullfile(file)); [name] = fileparts(file);
And it outputs the pathstring instead of the name. So I get
name = ''
However if I include pathstr as an output argument with name,
[pathstr, name] = fileparts(file);
I get the following.
pathstr = ''
name = (Whatever the name of file is)
I get what I want but with an added pathstr output argument which I don't need. Any way I can just parse out the name of the file only without the path string? Or will I just have to deal with having pathstr as an output in my workspace?
You can use ~ when you want to discard outputs (it doesn't mean that they are not computed, but this allows you not to declare variables for storing them).
[~, name] = fileparts( file ) ;
if you wanted the path and the extension, you would do
[pathstr, ~, ext] = fileparts( file ) ;
and if you wanted just the extension
[~, ~, ext] = fileparts( file ) ;
ooh ok gotcha. Thank you Cedric and Sean. It turns out including pathname as an output for uigetfile(), and as an input for fullfile() turned out to be really useful.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!