load global variable from startup.m

5 次查看(过去 30 天)
John
John 2019-12-4
编辑: Adam 2019-12-4
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 个评论
Adam
Adam 2019-12-4
编辑:Adam 2019-12-4
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.

请先登录,再进行评论。

回答(1 个)

Nicolas B.
Nicolas B. 2019-12-4
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.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by