MATLAB: How to take matrix from .txt file

importdata

Hi everybody! Attached there is my input file, inside txt file there are different table. I need to extract these matrix considering that format of input file is standard, but the matrix could be longer or smaller in different input file.

Best Answer

clc; clear all ;
file = 'walski10leakA.txt' ;
fid1 = fopen(file) ;
iwant = {'[JUNCTIONS]' ; '[PIPES]'} ; % your wanted data
S1 = textscan(fid1,'%s','Delimiter','\n'); % Scan the text file
fclose(fid1) ;
S1 = S1{1} ;
% Get the row index of your strings
pos1 = strfind(S1,iwant{1}) ; pos1 = find(~cellfun(@isempty,pos1)) ;
pos2 = strfind(S1,iwant{2}) ; pos2 = find(~cellfun(@isempty,pos2)) ;
% Get [JUNCTIONS] data
count = 0 ;
temp = iwant{1} ;
pos11 = pos1+1 ;
while ~isempty(temp)
pos1 = pos1+1 ;
temp = S1{pos1} ;
if ~isempty(temp) ;
count = count+1 ;
JUN_data{count} = temp ;
end
end
% Get [PIPES] data
count = 0 ;
temp = iwant{2} ;
while ~isempty(temp)
pos2 = pos2+1 ;
temp = S1{pos2} ;
if ~isempty(temp) ;
count = count+1 ;
PIP_data{count} = temp ;
end
end
This is one of the way..there could be other infinite ways....you can extract the data you want from cells JUN_data and PIP_data...
Good luck