I have data under headings with underscore.i e. tce500_a,tce500_b... How can I read data under tce500_c. The '_' is not recotnized and using t500 reads all of them.Thanks

t500_a
0 50
0.5 40
I want to read the data under each heading distinguished by _a,_b,_c, etc.

3 commentaires

I am not clear if you have multiple files, or if you have multiple variables, or if you are using xlsread() and you want to match the rows that have _c as part of their first column, or if you are using readtable and you want to match rows that have _c as part of their first column?
I have one text file which contains multiple headers which identify the data. The data consists of two columns, one is time and the other is concentration measurements. We have repeated experiments, so the header identifies each single set of columns. For example, t500_a contains two columns for the first experiment; t500_b contains two columns for the next experiment, etc. I want to read the entire txt file, find a specific header for the experiment I want, say t500_c, and then read the two columns of data under that specific header (experiment). It is a pretty big file with strings separating the data I want to read for a specific case. Thank you for your help.
Part of my question is how to deal with the underscore symbol. Thanks

Connectez-vous pour commenter.

 Réponse acceptée

S = fileread('YourFileName.txt');
[headers, block_start] = regexp(S, '^[^\n]*_[^\n]*', 'lineanchors', 'match');
block_bounds = [block_start, length(S)+1];
headers_to_extract = {'t500_c', 'ta719_d'};
num_to_extract = length(headers_to_extract);
extracted_data = cell(num_to_extract, 1);
[tf, blkidx] = ismember(header_to_extract, headers);
for K = 1 : num_to_extract
if ~tf(K)
fprintf('Warning: header "%s" not found in file\n', headers_to_extract{K});
else
thisidx = blkidx(K);
thisblock = S( block_bounds(thisidx) : block_bounds(thisidx+1) - 1 );
extracted_data(K) = textscan(thisblock, '%f %f', 'HeaderLines', 1, 'CombineOutput', 1); %note rhs is a cell
end
end
The result will be a cell array, extracted_data, with as many entries as was in the list headers_to_extract, each entry of which is the relevant data from the file. In the case where the header was not found in the file, the entry will be empty. The cells will be in the same order as headers_to_extract

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by