how do i load 100 txt files to a cell array and use specific data entries in each file to build a variable vector

1 view (last 30 days)
how do i load 100 text files (1height-0000, 1height-0001, 1height-0002,1height-0003, 1height-0004 ...................) to a cell array and access the entries on the 18th row of first column. i want to create a new variable called height with each entry making up a 100 x 1 column vector. The entries are space sperated as shown below:
The code i have written so far brings back an error...
clear
name = "1height-000%d.txt";
numfiles = 99;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf(name, k);
mydata{k} = importdata(myfilename);
end
I will appreciate an urgent assistance.....Thanks in Anticipation

Answers (1)

Jalaj Gambhir
Jalaj Gambhir on 12 Nov 2019
Hi,
Assuming you want to process all files and store the numerical value in the 18th line of each file to a 100x1 double vector, you can try the following code:
clear
name = "1height-000%d.txt";
numfiles = 100;
height = zeros(100,1);
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf(name, k-1);
fid = fopen(myfilename);
linenum = 18;
C = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
height(k) = str2double(C{:});
end

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!