The size of the indicated variable or array appears to be changing with each loop iteration.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I got this message : The size of the indicated variable or array appears to be changing with each loop iteration.
I want to make a matrix/array of a .txt file 2x150 the first column countain number 1 to 150 and the second a string corresponding at the address.
Here the file format :
So for example the result should be
[1, 1275 Avenue du College, Laval, QC, H7C 1W8]
[2, 1050 boul. David-Bouchard Nord, Granby, QC, J2G 5P3]
···
My code is :
function [source] = ChargerListeAddresse ()
fichier_a_lire = fopen('ListeBornes.txt', 'rt');
if fichier_a_lire ~= -1
nb_position = fscanf(fichier_a_lire, '%i\n', 1);
for i = 1:nb_position
donnees = fscanf(fichier_a_lire, 'id:%g - adresse:%s\n', 2);
source(i, 1) = donnees(1);
source(i, 2) = donnees(2);
fprintf('%s\n',source);
end
else
fprintf('Une erreur est arrivée avec l''ouverture du fichierListeBornes.txt');
end
end
What should I do?
2 Commenti
Risposte (1)
Shiva Kalyan Diwakaruni
il 26 Mar 2020
Hi,
1. What does this warning mean?
This code is correct in terms of syntax and it will execute correctly returning the expected result: the i-th element of source will contain the value donnees( i ).
However, before this small piece of code runs, the variable
source is not defined. Now, when the loop starts, source(1,1) is assigned the value donnees( 1 ), and so MATLAB creates source as a length-1 array. At the second iteration source(2) is assigned the value donnees( 2 ) and so MATLAB needs to change source to be of length 2, and so on: source changes its length/size at each iteration.
2. Why is changing variable size every iteration is a bad thing?
Consider what happens in the background (in terms of memory allocation) when source changes its size every iteration: At each iteration MATLAB needs to find a free memory space to host the new size of SOURCE. If you are lucky, there is enough free space right after source so all that happens is a change to the amount of memory allocated to source and writing the new value at the right spot.
However, if there is not enough free space just after
source, MATLAB has to find a new spot for all the i-1elements already in source, allocate this new space for source, copy all i-1 values already in source to the new spot, and free the old spot source used. This allocate-copy-free operations happening in the background can be extremely time consuming, especially when source is large .
This problem can be solved in two ways.
- Prepopulating the source array and using it.
- Iterating from highest index to lowest index.
Following code can help you solve your problem.
function[source] = ChargerListeAddresse ()
fichier_a_lire = fopen('ListeBornes.txt', 'rt');
if fichier_a_lire ~= -1
for j=150:-1:1
thisline = fgetl(fid);
aftersplit=regexp(thisline,'-','split','once');
source{j,1}=strtrim(aftersplit{1}(4:end-1));
source{j,2}=strtrim(aftersplit{2}(:));
end
else
fprintf('Une erreur est arrivée avec l''ouverture du fichierListeBornes.txt');
end
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings 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!