MATLAB Answers

load global variable from startup.m

2 views (last 30 days)
John
John on 4 Dec 2019
Edited: Adam on 4 Dec 2019
Is it possible to dfine a global variable using ...\Documents\MATLAB\startup.m? I would like to define frequently used variables and have them available after launching MATLAB.

  1 Comment

Adam
Adam on 4 Dec 2019
I just define a class containing constants I want easy access to, e.g.
classdef Constants
properties( Constant, Access = public )
% File size stuff
OneGigabyte = 1073741824;
OneMegabyte = 1048576;
OneKilobyte = 1024;
% Degree/Radian conversion
RadiansToDegrees = 57.2957795130823;
DegreesToRadians = 0.0174532925199433;
% Frequency analysis
Nyquist = 0.5;
...
end
end
I don't define many so I'm happy enough with them all in one file, but if I had loads I would likely define numerous classes, e.g. FileConstants, FrequencyConstants, etc, etc.
I can just call these anywhere in code as e.g.
myArray = rand( 300, 400, 500 );
arraySizeGB = size( myArray ) * 8 / Constants.OneGigabyte;
etc.
Most things I have defined in classes that they are relevant to, but these are just the odds and ends that are leftover in my case that I stick in one easy-access file. Most of them I rarely use, except those file size ones, almost exclusively for quick command line checking how much memory I would need for something or other.

Sign in to comment.

Answers (1)

Nicolas B.
Nicolas B. on 4 Dec 2019
I clearly don't recommend your to do so. Instead, I would add some functions in your standard Matlab path which returns your constants. Then, it will be accessible from any script/function/class you (and only you or the persons with your library) will create.
Example:
you have a variable which gives you the 0°K in °C:
function k0 = zeroDegKelvin()
k0 = -273.15;
end
If you do so. I would recommend you to create a toolbox from your function lists or to use git/svn/... to have them on a server. That way, you can easily install your functions on another pc or after a installation refresh of yours.

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by