Keep getting error message

4 visualizaciones (últimos 30 días)
Matthew Covington
Matthew Covington el 4 de Mzo. de 2019
Respondida: Walter Roberson el 4 de Mzo. de 2019
Above is the pompt we are to follow to get the correct output, using fopen to get the data from a different file.
%*****CONSTANT*****
NUM_MONTHS = 12;
FIRST_YEAR = 2013;
LAST_YEAR = 2016;
%*****INPUT*****
month = 1:NUM_MONTHS;
year = FIRST_YEAR:LAST_YEAR;
% read data file
FID = fopen('lake_powell.txt','r');
lake_powell(month,year) = fscanf(FID, '%f', 1);
fclose(FID);
%{
Unable to perform assignment because the indices on the left side are not compatible
with the size of the right side.
Error in assign06b (line 22)
lake_powell(month,year) = fscanf(FID, '%f', 1);
%}
Above is the code I currently have, followed by the error message. I don't understand what I am doing wrong, could someone please explain and put me on the right track?

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Mzo. de 2019
If the file is empty or does not start with a number, then fscanf() would return empty, and empty cannot be stored in a particular location.
Note: It is unlikely that you want to keep reading from the same location in the file. You probably do not want the fopen / fclose to be inside the loops.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by