Opening Sequential .txt Files and Populating Array with Data
Ältere Kommentare anzeigen
Hello All,
I would like to write a scrip that will open multiple tab delimited, headerless .txt files from a folder (ex file name "Test1" "Test2" etc) and have the second column copied into an array of zeroes. So far I have tried to follow a similar case I found here:
%This scrip is designed to open multiple .txt files containing %data and write the second column of data into an array.
%1)Set-up File Directory
%Copy folder path here:
MyFolder = Users/Chris/Desktop/Test;
TextFiles = dir(Users/Chris/Desktop/Test .txt);
%Create and array of zeros to rewrite data over
Absorbance = zeroes (2048, 63); %2048 x values and ## spectra files
%3)Import Absorbance Data
for i = 1:63 %Number of spectra files in directory
TextFileNames = ['Test_Absorbance_' num2str(k) '.txt'];
%Open Text File, 'rt' = read text (only works for txt files)
fid = fopen(TextFileNames, 'rt');
%Read file into 2 separate variables
Data = textscan (fid, '%d%d', 2);
%4)Wrtie Abs. data to array
%Insert absorbance data into array
Absorbance(:,k+1) = Data {2}; %put in 2nd row of data
fclose(fid);
end
the code-error alert icon in the upper right is green, however when I run the scrip I get an "undefined function or variable 'Users' error. I tried switching the path to ~Desktop\Test and get the same error. I would appreciate if someone could explain what the proper command should be.
Thank you in advance,
Akzeptierte Antwort
Weitere Antworten (2)
MyFolder = Users/Chris/Desktop/Test;
...the code-error alert icon in the upper right is green, however when I run the scrip I get an "undefined function or variable 'Users' error
Because you wrote the path w/o enclosing the string in quotes. Matlab thinks you're trying to divide four separate variables and store the result in the LHS variable MyFolder until run time it discovers there is no variable Users
You meant to write
MyFolder = 'Users/Chris/Desktop/Test';
BTW, I'd approach it slightly differently...use dir to return the files and iterate over the collection and avoid the grief of building file names manually...plus if one isn't there, you won't have to try to skip it--
Instead of
for i = 1:63 %Number of spectra files in directory
TextFileNames = ['Test_Absorbance_' num2str(k) '.txt'];
fid = fopen(TextFileNames, 'rt');
...
I'd suggest
d=dir('Test_Absorbance_*.txt'); % find the extant absorbance files
for i=1:length(d)
fid = fopen(d(i).name, 'rt');
...
Also NB: that if these files are in the directory as given above by MyFolder both of the above are going to fail unless that is your working directory. If that is the case, use fullfile to build the filename for dir and the fully qualified name in the fopen call
1 Kommentar
ADDENDUM
BTW, here's the kind of place where old-fashioned textread is a real improvement actually over the newer textscan -- it's unfortunate imo that TMW has so strongly deprecated its use.
d=dir('Test_Absorbance_*.txt'); % find the extant absorbance files
for i=1:length(d)
data = textread(d(i).name);
...
You get a way to address the file directly w/o dealing w/ the file handle and a friendly data array directly instead of the encapsulating cell that there's no need for.
Chris
am 29 Apr. 2014
0 Stimmen
Kategorien
Mehr zu Cell Arrays finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!