Reading numerical data from text fles with string

Dear all,
I will really appreciate f you help me out in this regards. I am using follwing code to read multiple text files and genrating a data based on specific columns. I now wanted to read the numeric values from line 10 and 11 and place them in first and column of data and repeat them for all the values of a specific cell array. Based on new cell array, the values should change till the end of that array and so on.....
clear;
D = 'T:\Intelli_test\New\files';
S = dir(fullfile(D, '*.txt'));
N = length(S);
% A = cell(N,1);
fmt = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r');
A(k)= textscan(fid, fmt, 'Delimiter', '\t', ...
'headerlines', 33, ...
'CollectOutput', 1);
fid=fclose(fid);
M = vertcat(A{:});
out = M(:,[9,11]);
data=out;
end
i am now trying to add a loop like this to get the values but i am getting nothing
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r');
linenum = 10;
h(k)= textscan(fid,'%f',1,'delimiter','\n', 'headerlines',linenum-1);
fid=fclose(fid);
end

댓글 수: 1

Note that this line
M = vertcat(A{:});
and the ones that follow it until the end of the loop should go after the loop.
There is no point in calling them inside the loop like that.

댓글을 달려면 로그인하십시오.

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 2월 1일
hello
my suggestion for the second part of your code , assuming you specify the lines to be read :
n_lines = [10 11]; % define lines to read
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r'); % open file
result = cell(1,numel(n_lines));
for n = 1:numel(n_lines)
result(n) = textscan(fid, '%s', 1, 'Headerlines', n_lines(n)-1, 'Delimiter' ,'');
frewind(fid) % set file position back to the start
end
result = [result{:}]; % unbox from cells
fclose(fid); % close file
end

댓글 수: 6

Many thanks for your answer but i am not being able to loop over all text files. Further, i was only interested in numeric values of temperature and strainrate and not the complete string'Temperature: 250 °C': Do you have any suggestion how i can parse the string and only obtain numeric values
oops sorry, I'll try to fix that tomorrow
so , now i hope it works better :
see the results in array result
n_lines = [10 11]; % define lines to read
result = zeros(N,numel(n_lines));
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r'); % open file
for n = 1:numel(n_lines)
str = textscan(fid, '%s', 1, 'Headerlines', n_lines(n)-1, 'Delimiter' ,'\n'); % change %s (string) to %f to get first numerical value (does not work ??)
tmp = regexp(char(str{1}),'-?\d+\.?\d*|-?\d*\.?\d+','match'); % extract numerical content of string
result(k,n) = str2num(tmp{:});
frewind(fid) % set file position back to the start
end
fclose(fid); % close file
end
Once again many thanks...........
Giving me an error like
Not enough input arguments
Error in Str2num
if ~ischar(s) || ~ismatrix(s)
can you share th txt file that generates this error ?
can you copy the output of whos from your workspace ? I'm interested to see what is in vraibales str and tmp ...
tx
i have attached the files and whos
Name Size Bytes Class Attributes
A 9x1 970344 cell
D 1x25 50 char
M 6732x18 969408 double
N 1x1 8 double
S 9x1 8061 struct
data 4386x2 70176 double
fid 1x1 8 double
fmt 1x36 72 char
k 1x1 8 double
n 1x1 8 double
n_lines 1x2 16 double
out 6732x2 107712 double
result 9x2 144 double
str 1x1 258 cell
tmp 0x0 0 cell

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

fadzhi
fadzhi 2021년 2월 2일

0 개 추천

I found out that the units are problematic....I want to extract Dehnrate which also has a unit 1/s, 'Dehnrate: 10.0 [1/s] ', so i think somehow this 1 is problematic because it works for other quantities

댓글 수: 4

ok , so Ifixed the problem by selecting only the first numerical field
tested for lines 6 to 11 all together - so far so good :
n_lines = (6:11); % define lines to read
result = zeros(N,numel(n_lines));
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r'); % open file
for n = 1:numel(n_lines)
str = textscan(fid, '%s', 1, 'Headerlines', n_lines(n)-1, 'Delimiter' ,'\n'); % change %s (string) to %f to get first numerical value (does not work ??)
tmp = regexp(char(str{1}),'-?\d+\.?\d*|-?\d*\.?\d+','match'); % extract numerical content of string
tmp2 = tmp(1);
result(k,n) = str2num(tmp2{:});
frewind(fid) % set file position back to the start
end
fclose(fid); % close file
end
this is another alternative using extractBetween , if you have release above R2017a
n_lines = (6:11); % define lines to read
result = zeros(N,numel(n_lines));
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r'); % open file
for n = 1:numel(n_lines)
str = textscan(fid, '%s', 1, 'Headerlines', n_lines(n)-1, 'Delimiter' ,'\n'); % change %s (string) to %f to get first numerical value (does not work ??)
tmp2 = extractBetween(char(str{1}),':','[');
result(k,n) = str2num(tmp2{:});
frewind(fid) % set file position back to the start
end
fclose(fid); % close file
end
Many thanks...working fine
Glad it helped !

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2021년 2월 1일

댓글:

2021년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by