How do I read data from a filename called in code or make the filename a string?
8 views (last 30 days)
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?
0 Comments
Accepted Answer
Fangjun Jiang
on 18 Jul 2018
If you select multiple files, "files" is returned as a cell array, you need to use
file=files{1}
5 Comments
More Answers (2)
dpb
on 18 Jul 2018
Edited: dpb
on 18 Jul 2018
[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
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
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 .
See Also
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!