Preserve Name of Indexed Variable for columns when creating a Table

As title says i want to creat a table that contains different values.
If i write
T = table(Time,Data,Etc);
the names of the columns will be
Time Data Etc
BUT if i write
T = table(Time(1:50,:),Data(1:50,:),Etc);
the names of the columns will be
Val1 Val2 Val3
So my question is, how can i preserve the names of the variables when indexing them? I know there are some methods to do this by creating new variables with the indexed value or change the name of the columns with the properteries option. But this is an additional step i have to make and im lazy.

Risposte (1)

TS - you can set the variable names when you create the table
T = table(Time(1:50,:),Data(1:50,:),Etc(1:50,:), 'VariableNames', {'Time', 'Data', 'Etc'});

7 Commenti

Hi Geoff,
thanky you for the answer. But as i mentioned, this would require additional effort wich i would like to skip. Specially if you have a huge amount of data to process, write every Column name can take a lot of time. Thus it would by very convinient if matlab would still name the columnes by variable name, not matter if indexed or not.
Further note:
When you pass an entire variable into a function, the name of the variable can be detected using inputname() .
However, when you pass an expression into a function, the components of the expression cannot be detected: inputname just returns empty for that position.
In order for there to be the possibility that table() could somehow detect that you passed in Time(1:50,:) and have it figure out that you might want to call the variable Time, then either MATLAB would have to be extended, or else table() would have to use debugging facilities to find a copy of the line of code that invoked table() and parse it to figure out what the (probable) variable name should be. Including handling cases like a command split over multiple input lines, or a command being eval()'d.
So.. either use 'VariableNames' option, or assign to Properties.VariableNames, or else assign the indexed expressions to variables named the same as you want the variables to be named.
But this doesn't make sense to me. Either way, passing an entire variable to the function or tell the function to just use a fractio of it, the varaible still stay the same as well as the link to the variable is still the same.
No, when you index a variable, a temporary array is constructed. I talk about the process more in
When you pass Time(1:50,:) into a function, then it is not the case that MATLAB passes a pointer to the beginning of the array and passes the indices with something in the handling of the called code processing the indexes. That would get messy very quickly -- since the received variable might get indexed and passed into something else, so multiple layers of indexing information would have to get sent around if it was information about how to do the indexing that was being passed.
When you pass Time(1:50,:) into a function, the handling is the same as if you had passed Time + 1 into the function, or some other expression where passing variable and indexing information would not be enough even in principle. Instead, the Time(1:50,:) and Time + 1 cases are treated the same: the results of the expression are calculated, creating a new unnamed temporary variable, and the unnamed temporary is what is passed in. Either way the receiving function doesn't know the name of the expression because expressions do not have names.
The only time the receiving function can find out the name of the passed variable is if an entire named variable is passed, plain, with no calculations involved.
Indexing to read data is a function call in MATLAB: it returns the actual data, not a reference to a variable together with information about how you would index the variable if it turns out you need the information stored there.
(Indexing to write data is a function call in MATLAB too.)
When a "plain" variable is passed, obviously the label of the variable also travels along for the ride in some sense, not just the values that the label represents. Otherwise, inputname() couldn't work at all.
Therefore, it makes no sense to me that if/when an indexed variable is passed, that a string like "varName(1:5,:)" couldn't also go along for the ride, or be temporarily created just like the new temp array of values is created, to be able to be grabbed by inputname() or a similar grabber.
If Time(randi(height(Time),10,1) were to be passed in, then should the "name" that goes along be "Time(randi(height(Time),10,1)" or should it be "Time([61;80;6;13;49;40;77;61;46;6],1)" ? And what should be the corresponding name of the table variable that should be generated ?
Inputnames() should be able to get at a "string screenshot" of whatever was actually input.
As if the user was separately prompted to type in what they wanted each input argument to be, and whatever they typed in was saved as a string. That's plenty good enough. If you need the actual numbers of the indices, you can do that in the function and remake the string that gets displayed. You can't do anything now.
My context is a function that analyzes data and then plots results. It seems unreasonable to me to have to make a call like:
analyzeDataAndPlotResults(overallData(bestN,[4 5 7 8]),"overallData(bestN,[4 5 7 8])")
in order to have "overallData(bestN,[4 5 7 8])" displayed on the analysis figures.
The new (temp) array of the actual values of verallData(bestN,[4 5 7 8]) can continue to be labeled whatever they are labeled now. So long as inputnames can get at that "overallData(bestN,[4 5 7 8])".

Accedi per commentare.

Richiesto:

TS
il 24 Mar 2021

Commentato:

il 22 Nov 2023

Community Treasure Hunt

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

Start Hunting!

Translated by