Leave out dot notation in table variables

Hey guys,
I am using quite a lot of functions that require the variables from a table "D". Because of that, every function has the input parameter "D". Since I do not want to redefine the variables at the beginning of each function, my question is as follows:
When it comes to calculation, it currently requires me to write "D.Speed", "D.Time" etc. instead of "Speed", "Time". Is there a way to avoid the "D." notation. In VBA, for example, you can code
% With D
% Speed....
% Time...
% end
I could not find an equivalent method in Matlab.
Your help is highly appreciated!

5 Kommentare

Stephen23
Stephen23 am 19 Jun. 2019
"Since I do not want to redefine the variables at the beginning of each function"
This is very unclear: the names of any input arguments to a function (e.g. tables) is totally irrelevant to the names of variables you might use inside your function. As long as you are passing your data properly (i.e. as input and output arguments) then why does it matter what the table is called inside your function?
The name is totally irrelevant. It is just the fact that I have tons of variables inside the function and want to get rid of dot notations.
As I said: In Visual Basic for Applications you can simply write
With D
in advance so that it is clear that all variables used in the code belong to the table (so you do not have write "D.Speed" anymore but only "Speed"). And I would like to know if such an option also exists in Matlab.
Stephen23
Stephen23 am 19 Jun. 2019
Bearbeitet: Stephen23 am 19 Jun. 2019
"In Visual Basic for Applications you can simply write..."
MATLAB is not VB.
What is recommended, efficient, or considered to be good practice in VB does not mean that it will be efficient or good practice in MATLAB. And what you are asking about is a very bad practice in MATLAB and should be avoided.
".. I would like to know if such an option also exists in Matlab."
There are certainly ways to do this, but they are slow, complex, and very inefficient. The code that you are using now is simple, easy to debug, and very efficient. You should not change what you are doing.
Evan Droz
Evan Droz am 14 Feb. 2021
Why are you trying to correct someone's use of code and wasting their time instead of answering their question, or better yet, not saying anything at all?
Walter Roberson
Walter Roberson am 14 Feb. 2021
We have enough experience to know that the available methods to do what was asked, end up taking more time; and thus, the suggestions made to not do what was asked are suggestions aimed at having the poster not waste time.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

madhan ravi
madhan ravi am 19 Jun. 2019
Bearbeitet: madhan ravi am 19 Jun. 2019

0 Stimmen

I don’t know why it’s hard for you to use "D." , see https://in.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html to access table datas in different ways.
Walter Roberson
Walter Roberson am 19 Jun. 2019

0 Stimmen

The closest MATLAB equivalent is import but it only applies for packages https://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html not for tables .
There are ways to do it dynamically, but those ways are error prone .
You should probably just do a bunch of search-and-replace to add in the table name.

3 Kommentare

Dario Walter's "Answer" moved here:
Thanks for your replies. @madhan ravi: It simply makes the code hard do read beacause you always have to include the "D." notation. A little excerpt from my code:
function D= Mode1_Calc(D)
D.Mode1_Outer_s=nan(height(D),1);
D.Mode1_Inner_s=nan(height(D),1);
for i=1:height(D)
%dbstop in Mode1_Calc at 16 if i>=31
InterSectLineX(i,:)=[D.Sinkrate_fpm(i) D.Sinkrate_fpm(i)];
InterSectLineY(i,:)=[D.RadioAlt_ft(i) 0];
end
end
@Walter Roberson: So you would leave it as it is?
Thanks a lot!
Stephen23
Stephen23 am 19 Jun. 2019
"It simply makes the code hard do read beacause you always have to include the "D." notation."
To me that notation makes it clearer to read, because then I can tell immediately where each variable is defined (which in turn this makes code easier to write, debug, and maintain). And this advantage applies to the MATLAB JIT compiler just as much as it does to me!
What you are trying to do is certainly possible, but it would be complex, liable to bugs, and inefficient. Read this to know why:
"So you would leave it as it is?"
Yes, your code is fine just as it is.
Dario Walter
Dario Walter am 19 Jun. 2019
Thank you Stephen!

Melden Sie sich an, um zu kommentieren.

Produkte

Tags

Gefragt:

am 19 Jun. 2019

Kommentiert:

am 14 Feb. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by