Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calling the Correct Global Variable

Subject: Calling the Correct Global Variable

From: Jason

Date: 29 Aug, 2013 05:57:05

Message: 1 of 3

I am trying to call the correct values from a global variable using user inputs. First off here is my code:

clear all
clc
 wsectionz;
 
 prompt = 'What is the Nominal Depth (in) Parallel to the Flange? ';
 Nom_D = input(prompt);
 
 prompt = 'What is the Weight in Pounds Per Foot of Length? ';
 W_sec = input(prompt);
 
 a = {'W'};
 b = {'x'};

 c = strcat(a,int2str(Nom_D),b,int2str(W_sec))
 
d =get_section_property(W,***Help Here***);



***Help Here*** is where I am having trouble. All the ways I have tried to call the correct information come back as errors. The output for "c" is the information I am trying to use from global variable W.

"get_section_property" is the function that requires the correct input.

"wsectionz" contains the different values for my global variable, W. I defined W as follows:

global W
W= {'W36x300',[36.74,16.66,0.95,1.68,88.3,20300,1300,64.2,300,1244,244];
'W36x280',[36.52,16.6,0.89,1.57,82.4,18900,1200,52.6,280,1157,226];
'W36x260',[36.26,16.55,0.84,1.44,76.5,17300,1090,41.5,260,1064,207];
'W36x245',[36.08,16.51,0.8,1.35,72.1,16100,1010,34.6,245,997,193];
'W36x230',[35.9,16.47,0.76,1.26,67.6,15000,940,28.6,230,931,179];

My code can combine user inputs to create a string with the correct global variable title, but I can't figure out how to use this to call the correct information. For example, I can get "W36x280" as an output, but I need help to get the function "get_section_property" to pull this information from global W.

Thank you for any help/advice

Subject: Calling the Correct Global Variable

From: Steven_Lord

Date: 29 Aug, 2013 14:17:13

Message: 2 of 3



"Jason " <sloshy36@gmail.com> wrote in message
news:kvmnrh$qgk$1@newscl01ah.mathworks.com...
> I am trying to call the correct values from a global variable using user
> inputs. First off here is my code:

Don't use global variables. You're asking for trouble by doing so,
particularly with a global variable with so "simple" a name. [Hint: ANY
program you run in MATLAB has access to, and can change or delete, that
global variable without your knowledge. If that happens your program will
stop working and determining why the variable has been modified or deleted
(and which program did it) could be very time-consuming and frustrating.]

> clear all
> clc
> wsectionz;
>
> prompt = 'What is the Nominal Depth (in) Parallel to the Flange? ';
> Nom_D = input(prompt);
>
> prompt = 'What is the Weight in Pounds Per Foot of Length? ';
> W_sec = input(prompt);
>
> a = {'W'};
> b = {'x'};
>
> c = strcat(a,int2str(Nom_D),b,int2str(W_sec))
>
> d =get_section_property(W,***Help Here***);
>
>
>
> ***Help Here*** is where I am having trouble. All the ways I have tried to
> call the correct information come back as errors. The output for "c" is
> the information I am trying to use from global variable W.
>
> "get_section_property" is the function that requires the correct input.
>
> "wsectionz" contains the different values for my global variable, W. I
> defined W as follows:
>
> global W
> W= {'W36x300',[36.74,16.66,0.95,1.68,88.3,20300,1300,64.2,300,1244,244];
> 'W36x280',[36.52,16.6,0.89,1.57,82.4,18900,1200,52.6,280,1157,226];
> 'W36x260',[36.26,16.55,0.84,1.44,76.5,17300,1090,41.5,260,1064,207];
> 'W36x245',[36.08,16.51,0.8,1.35,72.1,16100,1010,34.6,245,997,193];
> 'W36x230',[35.9,16.47,0.76,1.26,67.6,15000,940,28.6,230,931,179];

Create wsectionz as a function that _returns_ a struct array W.

function W = wsectionz
W.W36x300 = ...
W.W36x280 = ...
% Or you can use the STRUCT function directly.

Call wsectionz in your code as:

W = wsectionz;

Then use dynamic field names to access the fields.

desiredField = 'W36x280';
data = W.(desiredField);

Alternately, if all the depths and weights that you expect the user to input
are positive integer values that aren't too large (you're not storing data
about something that's 100000 deep), create W as a regular cell array:

W{36, 300} = [1, 2, 3, 4, 5];

Then there's no need to fiddle with forming a variable/field/identifier
name:

data = W{36, 300}

These have the added bonus of:

1) Having one place that is the true definition of your reference data. If
you need to add/modify data, you change it in that one function not anywhere
you stored "stuff" in W.
2) The only way to change the true definition of the reference data is to
modify the file. While it is theoretically possible for any other program to
modify the file using file I/O commands, that's a very deliberate action
that's hard to hide. Modifying a global variable is as simple as defining
your own global of the same name, and that's much less "evil looking" than
locating the data file, opening it, and writing to it.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Calling the Correct Global Variable

From: Jason

Date: 29 Aug, 2013 18:28:10

Message: 3 of 3

Thanks for the feedback, I'll try going about it a different way.

"Jason" wrote in message <kvmnrh$qgk$1@newscl01ah.mathworks.com>...
> I am trying to call the correct values from a global variable using user inputs. First off here is my code:
>
> clear all
> clc
> wsectionz;
>
> prompt = 'What is the Nominal Depth (in) Parallel to the Flange? ';
> Nom_D = input(prompt);
>
> prompt = 'What is the Weight in Pounds Per Foot of Length? ';
> W_sec = input(prompt);
>
> a = {'W'};
> b = {'x'};
>
> c = strcat(a,int2str(Nom_D),b,int2str(W_sec))
>
> d =get_section_property(W,***Help Here***);
>
>
>
> ***Help Here*** is where I am having trouble. All the ways I have tried to call the correct information come back as errors. The output for "c" is the information I am trying to use from global variable W.
>
> "get_section_property" is the function that requires the correct input.
>
> "wsectionz" contains the different values for my global variable, W. I defined W as follows:
>
> global W
> W= {'W36x300',[36.74,16.66,0.95,1.68,88.3,20300,1300,64.2,300,1244,244];
> 'W36x280',[36.52,16.6,0.89,1.57,82.4,18900,1200,52.6,280,1157,226];
> 'W36x260',[36.26,16.55,0.84,1.44,76.5,17300,1090,41.5,260,1064,207];
> 'W36x245',[36.08,16.51,0.8,1.35,72.1,16100,1010,34.6,245,997,193];
> 'W36x230',[35.9,16.47,0.76,1.26,67.6,15000,940,28.6,230,931,179];
>
> My code can combine user inputs to create a string with the correct global variable title, but I can't figure out how to use this to call the correct information. For example, I can get "W36x280" as an output, but I need help to get the function "get_section_property" to pull this information from global W.
>
> Thank you for any help/advice

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us