Date: Mar 25, 2013 4:04 PM
Author: Derek Goring
Subject: Re: Difficulty reading specific information from a .txt file

On Tuesday, March 26, 2013 8:49:06 AM UTC+13, Miguel  wrote:
> I'm having trouble reading certain specific information from a .txt file. This contains a 48x48 matrix (element stiffness). Each entry is preceded by the row and colomn information like [ row, colomn]:
>
>
>
> I'd like to generate a 48x48 matrix on matlab removing everything that is not an entry value from the text file.
>
>
>
> A segment of the matrix is for example:
>
> Matrix:
>
> [ 1, 1]: 1.220e+002 [ 1, 2]: 4.688e+001 [ 1, 3]: 1.220e+002 [ 1, 4]: 0.000e+000 ...
>
> [ 2, 1]: 4.688e+001 [ 2, 2]: 7.754e+001 [ 2, 3]:-7.353e+000 [ 2, 4]: 0.000e+000 ...
>
> [ 3, 1]: 1.220e+002 [ 3, 2]:-7.353e+000 [ 3, 3]:-1.811e-014 [ 3, 4]: 0.000e+000 ...
>
> [ 4, 1]: 0.000e+000 [ 4, 2]: 0.000e+000 [ 4, 3]: 0.000e+000 [ 4, 4]: 0.000e+000 ...
>
> ...
>
>
>
> When the values are positive there's a space between the ]: part and the number. Example:[ 2, 1]: 4.688e+001
>
>
>
> The main issue is the negative values where the space has been replaced with a minus sign like for example: [13, 1]:-4.023e+000. I don't know in advance where the negative values are located.
>
>
>
> The code I have so far goes like:
>
>
>
> fid = fopen('Stiff_elem.txt');
>
> D = textscan(fid, '%s',1);% Jumps first line
>
> for i=1:48
>
> C{1,i} = textscan(fid, '%s',N);
>
> end
>
> fclose(fid);
>
>
>
> When collection the information with textscan I've been unable to obtain the negative values without collecting the colomn number and ]: as well which means N varies from row to row and therefore I don't know which entries of D contain the important information.
>
>
>
> Does anyone know how to solve my problem?
>
>
>
> Thanks in advance for the attention!


Why not use : as the delimiter:
fmt=repmat('%s%f',1,48); % Read each row completely
c=textscan(fid,fmt,...
'delimiter',':',...
'headerlines',1);
Now c will contain the whole matrix, including the [..]s which you can discard.