Using popupmenu to select and load files
11 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
David Barry
2016-12-15
3 个评论
David Barry
2016-12-15
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!