Getting "Subscripted assignment dimension mismatch" error message
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sebastian Ciuban
le 10 Avr 2014
Commenté : Timothy
le 30 Juin 2014
I made a function that extracts from a text file some numeric data. I want my function to return my data in a matrix with 36 rows and N columns but for some reason, when i run my function it stops the reading on 6th row and gives me this error ''Subscripted assignment dimension mismatch". To arrange my data in the final matrix after reading the lines from the text file and converting them to numeric I wrote:
j=1;
while ~feof(fid)
line1 = fgets(fid);
data1 = str2num(line1(1:2));
data2= str2num(line1(4:5));
etc.
matrix(1,j)=data1;
matrix(2,j)=data2
.
matrix(36,j)=data36..
j=j+1
end
But as I said when I run the function it stops on 6th row..But if I delete from my code until matrix(5,j)=data5 it works just fine.
2 commentaires
Réponse acceptée
per isakson
le 10 Avr 2014
Modifié(e) : per isakson
le 10 Avr 2014
Your way of parsing the text file is laborious and error prone.
K>> Min = str2num(line1(17:19));
K>> Min
Min =
9 4
The value of Min is .<1x2 double>, which causes the error
K>> eph(6,j)=Min;
Subscripted assignment dimension mismatch.
where line1 is
8 13 03 11 17 59 44.0 5.066394805908D-06 2.273736754432D-13 .....
Replace
str2num(line1(17:19))
by
str2num(line1(16:18))
But the following line of code is also in error.
2 commentaires
Plus de réponses (2)
per isakson
le 12 Avr 2014
Modifié(e) : per isakson
le 13 Avr 2014
Your file is not easy to read and parse with Matlab
- The file is fixed width formatted. There are no delimiter between the values - one must rely on the positions.
- fscanf doesn't parse "D+01". However, textscan does(?).
- The file contains one header and many blocks of data. All the data blocks have the same format. The data "values" are 19 characters wide.
- Each block contains a number, datetime and 29 different values
- I trust that the format will not change. A description of the format used to write the file would have helped.
This is the beginning of the file. I've truncated the rows to avoid wrapping.
2.10 NAVIGATION DATA RINEX V
SPIDER V4,3,0,4633 NACLR 2013 03 13 00:04 PGM / R
2.1420D-08 7.4506D-09 -1.1921D-07 0.0000D+00 ION ALP
1.2288D+05 0.0000D+00 -2.6214D+05 1.9661D+05 ION BET
4.656612873077D-09 1.687538997430D-14 503808 1731 DELTA-U
16 LEAP SE
END OF
1 13 03 11 20 00 0.0 6.680842489004D-06 2.955857780762D-12 0.0000
8.700000000000D+01-3.934375000000D+01 4.114457219373D-09 2.0198
-1.996755599976D-06 1.641191076487D-03 1.329556107521D-05 5.1537
1.584000000000D+05-2.421438694000D-08-8.571340308443D-02-2.4214
9.607402942553D-01 1.195625000000D+02 3.658472115763D-01-7.8521
-8.357491088073D-11 1.000000000000D+00 1.731000000000D+03 0.0000
2.000000000000D+00 0.000000000000D+00 8.381903171539D-09 8.7000
1.511400000000D+05 0.000000000000D+00
2 13 03 12 00 00 0.0 4.219263792038D-04 1.477928890381D-12 0.0000
5.900000000000D+01-4.168750000000D+01 4.622692451051D-09-1.3010
-2.242624759674D-06 1.214739750139D-02 1.278705894947D-05 5.1536
1.728000000000D+05 2.700835466385D-07-1.057868232687D-01-2.5331
9.392481697649D-01 1.218125000000D+02-2.648953410445D+00-7.9846
-1.810789712620D-10 1.000000000000D+00 1.731000000000D+03 0.0000
2.000000000000D+00 0.000000000000D+00-1.769512891769D-08 5.9000
1.655400000000D+05 0.000000000000D+00
I've written a function, baia_read, which reads the file. It returns a structure and the data seems to have ended up in the correct place ( needs to be checked ).
>> baia = read_baia();
>> baia
baia =
PNR: [212x1 double]
datevec: [212x6 double]
SVclbias: [212x1 double]
SVcldrift: [212x1 double]
SVcldrift_rate: [212x1 double]
IODE: [212x1 double]
Crs: [212x1 double]
Dn: [212x1 double]
M0: [212x1 double]
Cuc: [212x1 double]
e: [212x1 double]
Cus: [212x1 double]
sqrt_a: [212x1 double]
toe: [212x1 double]
Cic: [212x1 double]
OMEGA: [212x1 double]
Cis: [212x1 double]
I0: [212x1 double]
Crc: [212x1 double]
Omega: [212x1 double]
Omegadot: [212x1 double]
Idot: [212x1 double]
L2codes: [212x1 double]
Gpsweek: [212x1 double]
L2Pflag: [212x1 double]
SV_accuracy: [212x1 double]
SV_health: [212x1 double]
tgd: [212x1 double]
iodc: [212x1 double]
tt: [212x1 double]
fi: [212x1 double]
>>
where
function baia = read_baia( filespec )
if nargin == 0
filespec = 'baia.13n';
end
% read the text file to a string vector
buffer = fileread( filespec );
% Strip off the header
buffer = regexp( buffer, '(?<=END OF HEADER\s+?\r\n).+$' ...
, 'match', 'once' );
buffer = strrep( buffer, 'D', 'e' );
% Regular expressions, which match the PNR-date-time strings.
% It could be made more specific. This matches a row of 22 spaces.
xpr_PRN = '([ 0-9]{2})';
xpr_dt ...
= '([ 0-9]{3}[ 0-9]{3}[ 0-9]{3}[ 0-9]{3}[ 0-9]{3}[ 0-9\.]{5})';
% Determine the beginning and end of the blocks of data
block_start = regexp( buffer, [xpr_PRN,xpr_dt] );
block_end = [ block_start(2:end)-1, length( buffer ) ];
nBlocks = length( block_start );
% Format of the PNR-date-time fields and the data. The code here
% is formatted to look similar to the chunk of data in the file.
frm_ndt = '%2f%3f%3f%3f%3f%3f%5f';
frm_val = cat( 2, '%19f%19f%19f\r\n' ...
, '%22f%19f%19f%19f\r\n' ...
, '%22f%19f%19f%19f\r\n' ...
, '%22f%19f%19f%19f\r\n' ...
, '%22f%19f%19f%19f\r\n' ...
, '%22f%19f%19f%19f\r\n' ...
, '%22f%19f%19f%19f\r\n' ...
, '%22f%19f\r\n' );
% The names of the variables in the order they appear in the block
% of data.
line1 = {'SVclbias','SVcldrift','SVcldrift_rate'};
line2 = {'IODE','Crs','Dn','M0'};
line3 = {'Cuc','e','Cus','sqrt_a'};
line4 = {'toe','Cic','OMEGA','Cis'};
line5 = {'I0','Crc','Omega','Omegadot'};
line6 = {'Idot','L2codes','Gpsweek','L2Pflag'};
line7 = {'SV_accuracy','SV_health','tgd','iodc'};
line8 = {'tt','fi'};
variable_names = ['PNR','datevec' ...
, line1,line2,line3,line4,line5,line6,line7,line8 ];
% Allocate memory for the result.
baia = cell2struct( repmat( {nan(nBlocks,1)} ...
, [1,length(variable_names)] ) ...
, variable_names, 2 );
baia.datevec = nan(nBlocks,6);
for ii = 1 : length( block_start ) % loop over all blocks
num = sscanf ...
( buffer(block_start(ii):block_start(ii)+21 ), frm_ndt );
baia.PNR(ii) = num(1);
baia.datevec(ii,:) = num(2:end);
num = sscanf ...
( buffer(block_start(ii)+22:block_end(ii)), frm_val );
for jj = 3 : length( variable_names ) % loop over all variables
baia.(variable_names{jj})(ii) = num(jj-2);
end
end
end
8 commentaires
Image Analyst
le 30 Juin 2014
You mean in the Answers forum rather than the newsgroup. Not as a comment anywhere in this (Ciuban's) post, but as a brand new post/question in the forum.
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!