How can I add data to an existing array when the dimension do not agree?
1 view (last 30 days)
Show older comments
How can I add to an existing array that has dimensions (1,80) when the line of data that I need to add is (1,161)? I keeping getting a size error. What I'm trying to do is use fgetl to read a RINEX file and store the data from it. The data in the first line is shorter then the 2nd line. After the first line the data characters remain constant so I just need to figure out how to size up the matrix without deleting the 1st line of data. datagps is the variable im using to store the data.
gps = 0;
if (txtLine(1) == 'G')
gps = gps + 1
dataGPS(gps,:) = txtLine % 1st line is 80 char
for nLine = 1:noLinesGPS
gps = gps + 1
txtLine = fgetl(fid_temp);
dataGPS(gps,:) = sprintf('%s %s', dataGPS, txtLine); % 2nd line and all proceeding lines are 161 char
% C1 = numel(dataGPS)
% GP = (C1:gps)
% GP(gps,1)= dataGPS
end
end
0 Comments
Accepted Answer
Stephen23
on 16 Jul 2020
Edited: Stephen23
on 16 Jul 2020
Just use indexing to specify how many elements you are allocating to. MATLAB will expand the array to fit:
>> B = ['ABC';'DEF']
B =
ABC
DEF
>> B(3,1:5) = 'Hello'
B =
ABC
DEF
Hello
For best results you should preallocate the array before the loop, or even just an empy array with the final number of columns.
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!