Cant index into a constant Property of a class

I am trying to index into a string array, that is stored into the property of a constants class i created. When i get to the line of code (below) where the call is made, it returns the entire array. But when i insert that line of code into the command window, it runs just fine.
if Data.isCellOpen(Constants.CellNames(1), index)

Respuestas (1)

Prince Kumar
Prince Kumar el 21 de En. de 2022
Hi,
You can index into string array just like normal indexing. Please look at the code below for reference:
classdef NamedConst
properties (Constant)
R = pi/180
D = 1/NamedConst.R
AccCode = '0145968740001110202NPQ'
str = ["Mercury" "Gemini" "Apollo";
"Skylab" "Skylab B" "ISS"]
end
end
Now you can use the class name NamedConst to access the property and index it.
disp(NamedConst.str(1))
This "disp" function gives Mercury as output.
For more information please refer the the documentation : Defining Class Properties with Constant Values
Hope this helps!

3 comentarios

Tyler Wright
Tyler Wright el 21 de En. de 2022
Thanks for the response. When I run the code that you wrote, and write the "disp" function in the Command Window, it returns "Mercury," as you said. But when i put the "disp" function into a script file, it returns the entire array of strings, not just "Mercury". And this is what i am seeing throughout all my code. The command window not matching what the script reads.
-Best Tyler
Prince Kumar
Prince Kumar el 28 de En. de 2022
Hi,
Can you please share the script if possible?
For the script for your code, i have the class made and the script has 1 line of code and it is the disp(...) code you wrote.
If you run this below as a script, it never will index into "NamedConst.str", unless you save it into an intermediate array (testArr) below
disp(NamedConst.str(1))
testArr = NamedConst.str;
for i = 1:numel(NamedConst.str)
% disp(NamedConst.str(i))
Temp = NamedConst.str(i)
Temp2 = testArr(1)
end

Iniciar sesión para comentar.

Productos

Versión

R2019b

Preguntada:

el 8 de Dic. de 2021

Comentada:

el 28 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by