How do I read data from a filename called in code or make the filename a string?

I am trying to write a program that allows the user to call file(s) and then import/read the data for analysis. I have been testing on just the first file since I select multiple. This is my code currently:
[files,pathname]=uigetfile('*.csv','Select One or More Files','MultiSelect','on');
file=files(1);
filepath=fullfile(pathname,file);
data=csvread(filepath);
Currently it says that my argument must contain a string, but I do not know how to have it read as a string since the variable itself is just text. Matlab even seems to read it as a string when I type filepath into the command window, the full filename appears in quotes.
My goal is to run a loop of all the files once it reads the data as I only need a single calculated value from each file.
Does anyone know how to select a file and read its data within the code? I know I can just copy and paste the filename instead of doing all this, but this would make it much more efficient.
Using Matlab R2014b for reference. Maybe older version requires something different?

 Accepted Answer

If you select multiple files, "files" is returned as a cell array, you need to use
file=files{1}

5 Comments

But NB: that even if 'multiselect' is 'on', if the user decides to only select one file, the return is NOT a cell array and the above will fail with
>> files{1}
Cell contents reference from a non-cell array object.
>>
@Jenn -- test by clicking and opening only one file...
Yeah, it creates an error if only one is selected. I can just write an if statement or case/switch statement in case only one file is selected. Thanks for catching that.
Found another possible solution. I can check if just one file selected with ~iscell(files). If =1 (true), then only one file selected. size(files,1)=1 regardless if 1 or more files selected. I would need to look at size(files,2) for number of files, but then if only one file selected, it gives the value for the number of characters in the filename.

Sign in to comment.

More Answers (2)

[files,pathname]=uigetfile('*.csv','Select One or More Files','MultiSelect','on').'; % NB: return column vectors
for i=1:size(files,1) % iterate over returned files
fname=fullfile(pathname,char(file(i)); % convert cellstr to char string, do-nothing for char()
data=csvread(fname);
...
It's a shame TMW can't bring the file-handling routines all into the same century with a consistent interface and update all them to accept cellstr or, better yet, the new strings class. These mismatches/inconsistencies and then being unable to use anything but char() is an anachronism and defeats purpose of "rapid development".
ADDENDUM/ERRATUM
OK, I stand corrected; at least some (not time to check everything) of the i/o functions including csvread have been updated to accept string class (but NOT cellstr) so now we have another beginnings of orphaning a class and breaking symmetry...some progress if the routines that return filenames will begin returning strings...otherwise still have to manually cast which defeats "rapid" and "user-friendly"...

1 Comment

Re: your addendum.
I think their mental model is string in -> string out; other in -> other out.
I've started using convertCharsToStrings in most functions to just always have strings. Yes it's extra work, but it makes everything else so much easier.
[varargin{:}] = convertCharsToStrings(varargin{:})
However, I'm also not writing code that needs to support verLessThan(current_release).

Sign in to comment.

If you're on a newer release, just blindly call string() on the output which will convert either a char or cellstr to string. You can then process is with all of great new string functionality.

4 Comments

But still have the problem of
>> fullfile("mydir","test.csv")
Error using fullfile (line 51)
String input not supported.
>>
which is where Jenn's error came from.
I do see that at least some of the i/o functions have been updated; I missed noticing I guess because am so ingrained never thought to change...
It is in 18a(!)
It sounds pretty lame, but that was my individual favorite 18a feature :)
Will dir and friends begin returning string arrays, then, "real soon now"? :)
dir works with strings in 18a:
d = dir(string(pwd))
It just doesn't return them yet. My hope is that they make something that returns a table, strings, etc. anyway like in this blog post .

Sign in to comment.

Categories

Asked:

on 18 Jul 2018

Edited:

on 19 Jul 2018

Community Treasure Hunt

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

Start Hunting!