How can I add data to an existing array when the dimension do not agree?

2 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2020-7-16
编辑:Stephen23 2020-7-16
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 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by