How do I read data from a filename called in code or make the filename a string?
Show older comments
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
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
Sean de Wolski
on 19 Jul 2018
Edited: Sean de Wolski
on 19 Jul 2018
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).
Sean de Wolski
on 18 Jul 2018
0 votes
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
dpb
on 18 Jul 2018
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...
Sean de Wolski
on 18 Jul 2018
Edited: Sean de Wolski
on 18 Jul 2018
It is in 18a(!)
It sounds pretty lame, but that was my individual favorite 18a feature :)
dpb
on 18 Jul 2018
Will dir and friends begin returning string arrays, then, "real soon now"? :)
Sean de Wolski
on 19 Jul 2018
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 .
Categories
Find more on Data Type Identification 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!