Accessing global variable from function

I have the following code:
function [ dirsOut, dirsPerTask ] = prepare( n )
global dirs;
numOfDirs = size(dirs,1);
dirsPerTask = round(numOfDirs/n);
end
dirs is defined as global:
EDU>> whos global
Name Size Bytes Class Attributes
dirs 2198x1 301616 struct global
status 0x0 0 double global
Got an error while calling function prepare:
EDU>> [o, n] = prepare(4)
Error in ==> prepare at 3 global dirs;
Probably I'm missing something ... Please assist.

댓글 수: 2

Vishal Rane
Vishal Rane 2012년 11월 29일
The error you have posted 'Error in ==> prepare at 3 global dirs;' seems incomplete.
Run lasterror command after you get that error and post the contents of the message field.
Jan
Jan 2012년 11월 30일
@Irene: Please post the complete error message.

댓글을 달려면 로그인하십시오.

답변 (2개)

dpb
dpb 2012년 11월 29일

0 개 추천

Remove the trailing ';' from the global statement--ML thinks dirs; should be a variable name but the semi-colon isn't valid character in a name
--

댓글 수: 1

Walter Roberson
Walter Roberson 2012년 11월 29일
I used to think that too, but I was wrong. It turns out that command/function duality ends lines at semi-colons.

댓글을 달려면 로그인하십시오.

dpb
dpb 2012년 11월 29일

0 개 추천

Hmmm....I did a test first, why did it error? Oh, drat. I mistyped 'global' as 'globals' and didn't notice; just that it errored on the expected line. Mea culpa...
OK, so what is problem w/ the posted function?

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2012년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by