MATLAB Answers

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)
Kingsley Aniagbaoso
Kingsley Aniagbaoso on 9 Nov 2019
Answered: Jalaj Gambhir on 12 Nov 2019
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

  0 Comments

Sign in to comment.

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

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019b