Is there code to recognize and allow me to browse and select CSV data?
Show older comments
I currently have a code that reads CSV data and allows me to search through it to find specific info. But, I have many different CSV files that I may want to examine and I dont want to have to put in the URL for each data file each time. Is there a function that I can use inside CSVread that opens up a browse window to allow me to select the data file I want to look at?
Answers (2)
Actually, building on Shameer's suggestion,
[file,path] = uigetfile('*.csv','Select a CSV file');
if ischar(file)
filename=fullfile(path,file);
S=uiimport(filename);
end
let's you "have your cake and eat it, too!" :) Use uigetfile for the file dialog and then pass the selected file to uiimport.
You could wrap this in an outer loop, to allow repetitive calls, but you'll have to be careful because the uiimport is asynchronous and the dialog box may reappear again before you're ready...I'm sure there must be some way around it but didn't have time to 'spearmint...
Good luck...
Or, of course, you can just live with the default file selection list and type *.csv in the selection box to get the list...
Shameer Parmar
on 22 Jun 2016
[filename, pathname] = uigetfile({'*.xls';'*.xlsx';'*.csv'});
4 Comments
Madison R
on 22 Jun 2016
Well, what error (show us all the red type)?
NB: Even if you fix whatever error this is, you're not going to see a preview window and the return variable fname_in is going to hold the content of the file selected which is a pretty bad name for the data; that's what you might use for the output of uigetfile.
As noted above, there is no preview feature in csvread; it does precisely what the name says, reads a csv file and returns the data, that's it. uiimport, otoh, will try to open the selected file in a preview pane before reading it which is apparently, from the initial posting at least, what you were looking for. It has the nit that can't specify only .csv files in the file mask for the file dialog (@), but afaik this is the only prepackaged programmable way in Matlab other than the manual preview at the UI which you said you wanted to eliminate...after that, it's "roll your own"...
(@) That is, it won't accept the file mask a la uigetfile but that doesn't mean you can't manually change that mask when the dialog opens. A submittal to TMW at www.mathworks.com support for an enhancement request to include that would seem reasonable to me.
Shameer Parmar
on 23 Jun 2016
@Madison:
your syntax is incorrect..
it should be as uigetfile('*.csv');
dpb
on 23 Jun 2016
Actually, there's not a syntax error that will cause an error; the prompt string looks a little funky with the trailing ';' that isn't needed as well as the cellstr curlies, but it works just fine. The other argument is a line-start count other than zero for csvread which is also legal.
Consequently, if the file that were selected is in the working directory and a valid .csv file with no more than one header line, the code as written would, in fact work (I just tested it, copied and pasted on such a file here).
But, as noted, he'll then have the content of the file in a inappropriately named variable (only a style issue of course) but the key point is it again won't open any preview window...
Categories
Find more on App Building in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!