Why do I get the error "Undefined function or variable"?

I am receiving the following error message, how can I resolve this issue?
fungsi6 = zeros(:,i1);
for i1=1:c
fungsi6(:,i1)=c(1,i1) + c(2,i1)*x(i1,:)' + c(3,i1)*lat' + c(4,i1)*lon' + c(5,i1)*dem'
end

13 Comments

Make sure that c, x, lat, lon and dem are all defined.
I have defined c, x, lat, lon and dem and it's coming from command window
Undefined function or variable 'zeros'.
Error in errorKoreksi_Sta (line 19)
fungsi6 = zeros(:,i1);
It is not recommended to try to name a variable "zeros" as that is the name of of an important function to allocate memory that is initialized to 0. In its function form you would never pass a : to it.
so what should i do to solve it?
Rename the variable zeros. The problem is not with this loop, but with how you are trying to define fungsi6. If you show us the 18 lines before this, we might be able to give more specific advice.
this is the full lines
clear;clc;
y1 = xlsread('E:\Skripsi\Data debit & klimatologi\Database Curah Hujan\observasi1986_1987.xlsx');y=y1(4:end,:);
x = xlsread('E:\Skripsi\Data debit & klimatologi\Database Curah Hujan\data chirps 1986-1987.xlsx'); % CHIRPS sesuai koordinat stasiun observasi
lat=y1(1:end,:); % Lintang
lon=y1(2:end,:); % Bujur
dem=y1(3:end,:); % Elevasi
%regresi linear berganda untuk mendapatkan koefisien setiap vaiabel
c = zeros(6,730); %730 nunjukin jumlah hari yg dipakai
for i=1:c
XX = [ones(size(x(i,:)))' x(i,:)' lat' lon' dem'];
c(:,i) = XX.\y(i,:)';
end
%%menghitung curah hujan dugaan stasiun
for i1=1:c
fungsi6(:,i1)=c(1,i1) + c(2,i1)*x(i1,:)' + c(3,i1)*lat' + c(4,i1)*lon' + c(5,i1)*dem'; %6 jumlah stasiun obs
end
%menghitung error curah hujan dugaan stasiun
errordata19861987 = y - fungsi6';
dlmwrite('E:\Skripsi\Bias Koreksi\ErrorChirpsSta_1986-1987.txt',errordata19861987','\t');
What exactly are you trying to do here?
fungsi6 = zeros(:,i1);
This is a different code from the one in your original question. So we have absolutely no idea which line or which code is causing the error.
Therefore, please confirm the exact code you are using and give us the entire error message without any modification. Just copy and paste everything in red.
this is the entire error message
Undefined function or variable 'zeros'.
Error in errorKoreksi_Sta (line 19)
fungsi6 = zeros(:,i1);
there is warning like
the variable 'fungsi6' appears to change size in every loop iteration
it appears when i didn't write fungsi6 = zeros(:,i1);
I doubt that anyone remembers what the .\ operator does so I would not use that without a comment
The line in the error message does not appear in the code you've posted. How do you explain that?
it appears when i delete the . in line 14 Undefined function or variable 'fungsi6'.
Error in errorKoreksi_Sta (line 24)
errordata19861987 = y - fungsi6';

Sign in to comment.

Answers (1)

When you use an array as the end point of a : operator the first element of the array is extracted and the rest are ignored. Your
for i1=1:c
Is the same as
for i1=1:c(1,1)
This would not execute even once if c(1,1) was not at least 1, and since you do not initialize the variable assigned to in the loop that would leave the variable undefined.
I see no reason to expect that c(1,1) should be at least 1.

Categories

Tags

Asked:

on 28 Feb 2018

Answered:

on 28 Feb 2018

Community Treasure Hunt

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

Start Hunting!