Variable declaration in startup.m not working in MATLAB -
i'm trying declare magic number constants need access in every script. made file load_constants.m in my_path looks like:
magic_number = 10; other_magic_number = 12; my startup.m looks this:
% add bunch of packages addpath ~/documents/matlab/some-package userpath(my_path) load_constants but when try access magic_number in command window:
undefined function or variable 'magic_number'. directly declaring variables in startup.m doesn't work either. how fix this?
this normal behavior, startup.m function , variables declare inside function local function (and vanish when going out of scope):
function [] = startup() %[ magic_number = 10; %% local variable %] use assignin function have magic_number visible base workspace:
function [] = startup() %[ assignin('base', 'magic_number', 10); % value visible 'base' workspace %] note having magic_number value visible not scripts functions also, may better create magic_number.m function , add path:
function [v] = magic_number() %[ v = 10; %] which can called without brackets (i.e. syntax variables)
Comments
Post a Comment