Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

"??? Undefined function or variable" in a function (should be easy...?)

Asked by Max

Max (view profile)

on 19 Jul 2013

Hi everyone,

I'm trying to write a function that will take a vector of results and summarize the data into several categories.

I'm having an issue that's really confusing me, here are the first few lines of my function:

function summwriter(resultLogMain, subjectID)
filename = strcat('Results/summData',subjectID,'.txt');
outfile = fopen(filename, 'wt');
numberOfEntries = length(resultLogMain(1,:));
cat1_ln = 0;
cat1_hn = 0;
cat1_rts = zeros(1, numberOfEntries);

then later I use a "for" loop to fill in some values:

for i = 5:numberOfEntries;
    %Faces have no influence
    if resultLogMain(9,i) == 2 && resultLogMain(11,i) == 2;
        cat1_rts(i) = resultLogMain(2,i);
        %Subject chose hard task
       if resultLogMain(1,i) == 1;
            %12% win trial
           if resultLogMain(12,i) == 0.12;
               cat1_ln = cat1_ln + 1;
               % 88% win trial
           elseif resultLogMain(12,i) == 0.88;
               cat1_hn = cat1_hn + 1;

and then calculate some percentages, averages, and standard deviations

cat1_lp = cat1_ln/(numberOfEntries);
cat1_hp = cat1_hn/(numberOfEntries);
cat1_rtav = mean(cat1_rts(:));
cat1_rtsd = std(cat1_rts(:));

These excerpts cover one category, there are actually four but I think if I can figure it out for one I can understand the issue. Right now when I run the script it returns the error:

??? Undefined function or variable 'cat1_ln'.

Even though cat1_ln is CLEARLY defined right before the loop to be preallocated and filled in.

Am I doing something very simple very wrong? Does it have something to do with defining a new variable within a function? Any help would be great!



Max (view profile)


No products are associated with this question.

3 Answers

Answer by per isakson

per isakson (view profile)

on 19 Jul 2013


    >> dbstop if error  

and inspect the Workspace window when the break occur

Here are some links on debugging in Matlab


per isakson

per isakson (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 19 Jul 2013

We see, that you define cat1_ln before the loop. I guess, that the error occurs inside the loop (this would have been cleared, if you post the complete error message, which contains the crashing line). Then it is obvious, that you either have a typo in the code (1 and l looks so similar) or that the already defined variable is removed by a clear.

Does "then later" contain calls of scripts, which use clear all?


Jan Simon

Jan Simon (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 19 Jul 2013

Put "workspace" at the beginning of your code

workspace; % Show workspace panel.

to show the workspace panel of variable (if it's not already showing). Then set a breakpoint and step line by line (F10 key) until you see cat1_ln appear, then disappear. Like Jan says - it probably disappears when you execute a clear.


Image Analyst

Image Analyst (view profile)

Contact us