Keep getting "Undefined variable" message

edited April 2011 in Science & Tech
Matlab keeps telling me that 'form' is undefined. I've included the primary function (form_letter) and the first subfunction (read_form, which is where 'form' is supposed to get defined). When I test read_form individually, 'form' gets defined and everything works, so the problem must be in the primary function, but I have no idea what it is. If it helps, in the editor window the mouseover text tells me that "the value assigned to variable 'form' might be unused."

I appreciate any help you can offer.

[php]
function [] = form_letter()

clear;
clc;

form = read_form();
structure = read_tag_sub();
result = replace_tags(structure);
output_letter(result)

end



function form = read_form()

form = '';

fid = fopen('form.txt', 'r');
if fid == -1
disp ('Could not open form.txt\n');
else
while feof(fid) == 0
%read in the string including newline one line at a time
a_line = fgets(fid);
%concatenate the line to the form
form = [form, a_line];
end
end

close_result = fclose(fid);
if close_result ~= 0
disp ('Could not close form.txt');
end

end
[/php]

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited April 2011
    on what line do you get the error?

    also, before you run your function, type

    dbstop if error

    to have matlab drop into the debugger, rather than just error out. it should help you debug.
  • edited April 2011
    I tried debugging using dbstop as you suggested, but it hasn't led me to figure out what's wrong.

    As I probably should have mentioned, the error occurs at line 6, right at the beginning of the primary function --> form = read_form(). It tells me that form doesn't get defined.

    Any ideas?
  • edited April 2011
    Never mind, figured it out.
Sign In or Register to comment.