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

1 回表示 (過去 30 日間)
Nolan Makatche
Nolan Makatche 2020 年 7 月 16 日
編集済み: Stephen23 2020 年 7 月 16 日
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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by