load global variable from startup.m
8 ビュー (過去 30 日間)
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.
Adam 2019 年 12 月 4 日
編集済み: Adam 2019 年 12 月 4 日
I just define a class containing constants I want easy access to, e.g.
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;
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;
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. 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.
you have a variable which gives you the 0°K in °C:
function k0 = zeroDegKelvin()
k0 = -273.15;
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.
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!