How can i change this to a multiselect loop?

I have this code file that works but i would like to change it to multiselect and run a loop to process the data.
function [M1,YT] = read6840(fid)
[FileName, PathName]=uigetfile({'*.asc','Individual DAS Files (*.asc)'},...
'Select 6840 Word File');
fid=fopen([PathName FileName]);
info6840=textscan(fid,'%10s',43);
data6840=textscan(fid,'%15s %5s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %s',15000);
fclose(fid);
% Record Information
DATE =char(info6840{:}(1));
DATE=[DATE(4:5) '/' DATE(7:8) '/' DATE(1:2)]; %convert to MM/DD/YY
RECORD =char(info6840{:}(40));
CH =char(info6840{:}(41));
S=char(info6840{:}(42));
%LOCKON =char(info6840{:}(43));
% Assign Variables to Names
for n=5:38
eval([char(info6840{:}(n)) '=data6840{' num2str(n-2) '};'])
end
time6840=(0:1:length(XDOT)-1)'*.05;
% Assign Discrete Variables to Names
ZW=char(ZW);
STAT=char(STAT);
BUS=char(BUS);
This is how i attempted to change it. Based on another bit of code I have that successfully runs multiselect loop
function [M1,YT] = read6840(varargin)
if nargin==0
[FileName,PathName] = uigetfile('*.asc','Select 6840 Word File ','MultiSelect','on');
if isnumeric(FileName)
%User clicked cancel or closed the window
return
end
else
FileName = varargin{1};
if nargin > 1
PathName = varargin{2};
else
PathName = cd;
end
if nargin > 2
OutPath = varargin{3};
if exist(OutPath,'dir') ~= 7
mkdir(OutPath);
end
else
OutPath = PathName;
end
end
FileName = compose('%g',FileName);
NumOfFiles = length(FileName);
GoodFiles = 1:NumOfFiles;
SN = zeros(NumOfFiles,1);
T = zeros(NumOfFiles,1);
H = zeros(NumOfFiles,1);
C = zeros(NumOfFiles,1);
CH = cellstr(repmat('C',NumOfFiles,1));
MC = cellstr(repmat('UNK',NumOfFiles,1));
WS = cellstr(repmat('UNK',NumOfFiles,1));
WD = cellstr(repmat('UNK',NumOfFiles,1));
for inx=1:NumOfFiles %Multifile loop begin
fid=fopen(FileName);
info6840=textscan(fid,'%10s',43);
% if ~strcmp('6840',info6840{:}(3))
% %Not a 6840 file... skip and move on.
% fclose(fid);
% GoodFiles(inx) = -1; %Remove this entry from the good files list.
% continue
% else %Valid 6840 file
% data6840=textscan(fid,'%15s %5s %20s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %s %s %s');
% fclose(fid);
%
% % check data6840 for good data
% for ii=1:size(data6840,2)
% if isempty(data6840{1,ii})
% %Not a good file... skip and move on.
% GoodFiles(inx) = -1; %Remove this entry from the good files list.
% continue
% end
% end
% clear ii
%
% %If we get this far, this is a good file.
% end
% data6840=textscan(fid,'%15s %5s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %s',15000);
% fclose(fid);
% Record Information
DATE =char(info6840{:}(1));
DATE=[DATE(4:5) '/' DATE(7:8) '/' DATE(1:2)]; %convert to MM/DD/YY
RECORD =char(info6840{:}(40));
CH =char(info6840{:}(41));
SN=char(info6840{:}(42));
%LOCKON =char(info6840{:}(43));
% Assign Variables to Names
for n=5:38
eval([char(info6840{:}(n)) '=data6840{' num2str(n-2) '};'])
end
time6840=(0:1:length(XDOT)-1)'*.05;
% Assign Discrete Variables to Names
ZW=char(Z11LW);
STAT=char(STAT);
BUS=char(BUS);
end

 Accepted Answer

It's not clear exactly what read6840 is intended to do, so I can only offer an outline of how it might look operating on multiple files:
function read6840(varargin)
% I don't know what the output(s) of this function are (M1 and YT are not
% defined in your code), but whatever they are, they can be initialized
% here to avoid 'Undefined' error in case the user clicks cancel and the
% function returns early.
if nargin == 0
% no inputs given; prompt the user to select some files:
[FileName,PathName] = uigetfile('*.asc','Select 6840 Word File ','MultiSelect','on');
if isnumeric(FileName)
% User clicked cancel or closed the window
return
end
else
% FileName given as input:
FileName = varargin{1};
if nargin > 1
% PathName also given as input:
PathName = varargin{2};
else
% PathName not given as input; use the current working directory:
PathName = pwd();
end
if nargin > 2
% OutPath given as input:
OutPath = varargin{3};
% if it doesn't exist, create it:
if ~isfolder(OutPath)
mkdir(OutPath);
end
else
% OutPath not given as input; use PathName:
OutPath = PathName;
end
end
% if one file was selected, then FileName is a character vector (a
% character vector might have been passed in as the first argument as
% well), so make it a cell array:
if ischar(FileName)
FileName = {FileName};
end
% get full-path file names:
FileName = fullfile(PathName,FileName);
% initialize these variables:
NumOfFiles = numel(FileName);
SN = zeros(NumOfFiles,1);
T = zeros(NumOfFiles,1);
H = zeros(NumOfFiles,1);
C = zeros(NumOfFiles,1);
CH = cell(NumOfFiles,1);
MC = cell(NumOfFiles,1);
WS = cell(NumOfFiles,1);
WD = cell(NumOfFiles,1);
% loop over files:
for inx = 1:NumOfFiles
% open the current file:
fid = fopen(FileName{inx});
% ... code to read the file ...
% close the current file:
fclose(fid);
% ... code to do whatever you're going to do with this file's contents ...
% ... e.g., populate one element of SN, T, etc.:
SN(inx) = % ...
T(inx) = % ...
H(inx) = % ...
C(inx) = % ...
CH{inx} = % ...
MC{inx} = % ...
WS{inx} = % ...
WD{inx} = % ...
% ... whatever else ...
end
% ... whatever else ...
end

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

on 12 Jan 2024

Answered:

on 12 Jan 2024

Community Treasure Hunt

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

Start Hunting!