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

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Stephen23
Stephen23 il 16 Lug 2020
Modificato: Stephen23 il 16 Lug 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.

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by