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