Effacer les filtres
Effacer les filtres

Using popupmenu to select and load files

18 vues (au cours des 30 derniers jours)
Hard Rocker
Hard Rocker le 15 Déc 2016
Commenté : Hard Rocker le 16 Déc 2016
Hi all,
Currently working on a code to allow a user access to a popupmenu wherein they can select the name of a .csv file, and read it in to MATLAB. Currently, I have the figure window set up, and a simple popupmenu. ( *Please note this is not being constructed using GUIDE, and I'd appreciate it if answers did not invoke this.* ) However, my code/popupmenu is unresponsive when trying to set this up as a callback function. Could someone please offer help? Current code is below (note: read_georoc.m is a separate, standalone function file), and the goal is this:
(1) have user select a directory.
(2) Get the names of data files in that directory.
(3) Let user select which data file to examine via popupmenu (PUM), and have that csv read in as 2 cell arrays; 1st has the column headers, 2nd has all data.
(4) In addition to a PUM to select the data file, there will be 2 more PUMs (PUM2 & PUM3) for user to select abscissa (x-value) and ordinate (y-value) of data to be plotted; this will be done by getting value (i.e., index) of selections in PUM2 & PUM3, and plotting that column of the data array. Data array is the one from when the current csv file was imported in Step 3.
(5) An axes object with graph plotted using step 4 inputs.
( *Note* ) Ideally, graph and Step 4 PUMs would reset if a new file is selected and read in.
(6) Model curves calculated (based on PUM2 & PUM3 selections) that will be compared to the real data.
% program geoplotxy.m
% declare global vars
global data colnames cfnames cpath cfile
%
p0 = get(0,'screensize'); % get current screen size
%
%%initialize GUI environment
% make (full-size) window:
hf = figure('position',[1, 1, p0(3), p0(4)]); % make full screen
%
%%get to data
% navigate to data directory:
uiwait(msgbox('Select data directory.'));
cpath = uigetdir;
%
% content in cpath directory:
dircont = dir(cpath); % structure array
%
% get file names in cpath:
dirnames = {dircont.name}; % 1-by-n cell array with content names
cfnames = {};
count = 1;
for j = 1:numel(dirnames)
if ~strcmpi(dirnames{j}(1),'.')
cfnames{count} = dirnames{j}; % remove '.', '..', '.DS_Store' if Mac
count = count + 1;
end
end
%
% make drop down menu with csv files to handle (1 at a time)
hcsv = uicontrol(hf,...
'style','popupmenu',... % 'position',[],...
'String',cfnames,...
'units','normalized',...
'Position',[0.1, 0.8, 0.2, 0.1],...
'Callback',@hcsv_CB);
%
function [data, colnames] = hcsv_CB(hObject,eventdata)
global data colnames cfnames cpath cfile
uselect = hObject.Value; % get user selection in popupmenu
cfile = [cpath, '/', cfnames{uselect}]; % get full file path given user selection in menu
[data, colnames] = read_georoc(cfile);
end
The function read_georoc (exists as read_georoc.m in same directory as the main script) is coded below, and reads in a csv file of specific format:
function [data, colnames] = read_georoc(cfilename);
%
% read in file
fid = fopen(cfilename);
cdata = textscan(fid,'%s','delimiter','\n'); % reads in each line as 1 string
fclose(fid)
%
% get header row
%
cnames = cdata{1}(1);
%
% find commas (delimiters)
cidx = strfind(cnames,',');
%
% assemble column names
colnames = {};
colnames{1} = cnames(1:cidx(1)-1);
for n = 2:size(cidx,2)
name = cnames(cidx(n-1)+1:cidx(n)+1);
colnames{n} = name;
end
colnames{n+1} = cnames(cidx(end)+1:end);
% above results in 1-by-n array w/ column names, where each col = 1 data column of an analyte in {data}
%
%%process data
% remove header lines
cdata = cdata{1}(2:end);
%
% remove empty rows
cdempty = find(~cellfun('isempty',cdata));
cdata = cdata(cdempty,:);
%
% find commas (delimiters)
cidx = strfind(cdata,',');
%
% find double quotation marks (commas inside quotes are NOT delimiters)
qidx = strfind(cdata,'"');
% examine each row to keep commas b/t quotes intact:
for j = 1:numel(cdata)
cdel = []; % idx of commas to be ignored when separating data
for k = 1:((numel(qidx{j}))/2) % consider quotes as pairs
low = qidx{j}((2*k)-1); % idx of open quote in jth row
high = qidx{j}(2*k); % idx of end quote in jth row
% find commas between quotes in jth row:
cdel = [cdel, find(cidx{j}>low & cidx{j}<high)];
end
cidx{j}(cdel) = []; % delete commas b/t quotes in jth row from idx
end
%
% break out data by column:
for m = 1:size(cdata,1) % for each row of data
firstcol = cdata{m}(1:cidx{m}(1)-1); % first col does not begin w/ ','
data{m,1} = firstcol;
for n = 2:size(cidx{1},2) % subsequent cols bounded by ','
entry = cdata{m}((cidx{m}(n-1)+1):(cidx{m}(n)-1));
if ~isempty(entry) % avoid error when strcmp to empty cell
if strcmpi(entry(1),'"') % if starts w/ quote marks
entry = entry(2:end-1); % remove enclosing quotes, add only info to cell
end
end
data{m,n} = entry; % add entry to cell
end
lastcol = cdata{m}((cidx{m}(n)+1):end); % lastcol doesn't end w/ ','
data{m,n+1} = lastcol;
end
%
end % function end

Réponses (1)

David Barry
David Barry le 15 Déc 2016
  3 commentaires
David Barry
David Barry le 15 Déc 2016
I don't understand. What is the difference between a uigetfile window and a popupmenu? You can filter uigetfile to only show csv files and you can allow users to select more then one at a time. You are already throwing a uigetdir so why not just replace that with a uigetfile and have them do everything in one operation?
Hard Rocker
Hard Rocker le 16 Déc 2016
A pop-up menu is actually a drop down list in a GUI window, ex (if I remember correctly):
hf = figure;
cstr = {'apples' 'pears' 'oranges'};
hpum = uicontrol(hf,'style','popupmenu','string',cstr);
Whereas uigetfile brings up a window with the directory tree and files therein (a la Finder on Mac). I'm trying to avoid all the clicking and clutter of uigetfile (I don't want things to keep popping up onto the screen every time you want to look at a different dataset in the same folder). That, and my program is meant to handle only one CSV at a time, so I don't want to allow uigetfile with possible multiple returns; popupmenu allows exclusive selection of one option.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Analysis dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by