How to convert the name of a table or cell array to string
11 views (last 30 days)
Show older comments
Dear Experts,
I have a cell array named "cat" and a table named "dog" So cat has a series of data within. Likewise for dog.
I just want to extract the name of the cell array (cat) and name of the table (dog) to be label in title when plotting a figure.
Any ideas? LL
0 Comments
Answers (3)
Star Strider
on 23 Sep 2016
Edited: Star Strider
on 23 Sep 2016
See if this does what you want:
cat = {rand(1, 10)}; % Create Data
dog = table(rand(1, 15)); % Create Data
varlist = whos; % Workspace Variables
var_class = {varlist.class}; % Classes
cell_idx = strcmp(var_class, 'cell'); % Logical Indices of Cell Arrays
cell_names = {varlist(cell_idx).name}; % Cell Array Variable Names
tabl_idx = strcmp(var_class, 'table'); % Logical Indices of Table Arrays
tabl_names = {varlist(tabl_idx).name}; % Table Array Variable Names
figure(1)
subplot(2,1,1)
plot(cat{:})
title(cell_names{1})
grid
subplot(2,1,2)
plot(table2array(dog))
title(tabl_names{1})
grid
EDIT — I forgot one was a cell and one was a table.
This works, but you will probably have to adapt it to your application.
2 Comments
Star Strider
on 25 Sep 2016
Cheerful’s ‘Answer’ is duplicated here —
Hi Expert
I think there is a problem. I have many tables when I run my program and it picks up all the table names. So it is quite easily to mess up the names when I simply index it by tabl_names(i)
thanks
Star Strider
on 25 Sep 2016
Edited: Star Strider
on 26 Sep 2016
If there are more than one table arrays in your workspace and you want to choose the one to plot, I would add a listdlg call.
You can’t make it completely automatic if the user has to choose the table to plot. Unless you already know the table array name (in which situation that part of my code is then irrelevant), you have to incorporate some sort of user interaction to choose the correct table, and listdlg is likely the most straightforward to program and use.
I leave the rest to you.
EDIT — (26 Sep 2016 at 03:04 Z)
I had to use eval, but this code will discover all the table arrays in your workspace, and allow you to choose the table array you want to plot. You may need to change the ‘td’ assignment and plot call to work with it:
cat = {rand(1, 10)}; % Create Data
dog = table(rand(1, 15)); % Create Data
cow = table(rand(1, 5)); % Create Data
varlist = whos; % Workspace Variables
var_class = {varlist.class}; % Classes
cell_idx = strcmp(var_class, 'cell'); % Logical Indices of Cell Arrays
cell_names = {varlist(cell_idx).name}; % Cell Array Variable Names
tabl_idx = strcmp(var_class, 'table'); % Logical Indices of Table Arrays
tabl_names = {varlist(tabl_idx).name}; % Table Array Variable Names
[choose_table, v] = listdlg('PromptString','Choose a table to plot:', 'SelectionMode','single', 'ListString',tabl_names);
vnv = {varlist.name}; % Variable Names Vector
vnn = strcmp({varlist.name}, tabl_names{choose_table}); % Variable Names Number
plot_table = eval(vnv{vnn}); % To Others: If There’s A Way To Do This Without ‘eval’, Please Share The Code
td = table2array(plot_table); % Evaluate Chosen Table And Return Numeric Values
figure(1)
subplot(2,1,1)
plot(cat{:})
title(cell_names{1})
grid
subplot(2,1,2)
plot(td)
title(tabl_names{choose_table})
grid
I leave it to you to adapt it to your needs.
Cheerful
on 25 Sep 2016
3 Comments
Stephen23
on 27 Sep 2016
Edited: Stephen23
on 27 Sep 2016
@Cheerful: do you know why this is so hard? Because this is very bad program design. Even beginners should be able to understand that when a program algorithm and the data are well designed, then the code is simpler and more robust. Imagine that I put my data in nested cell arrays and complicated nested structures: this is going to be much harder to write code for (as well as being slower and buggier) than if I simply put my data into a simple numeric array.
You have chosen to design a program in a very buggy and pointlessly complicated way. Now you are finding out that, because of this bad design, it is hard to write the code for it. This is not something specific to MATLAB, because good design is a requirement for good code in any programming language.
You really need to learn more about good practices for writing code, and how to create robust algorithms.
Using indices is robust, and there are other methods too. Read these:
"Each csv file is already named in other system. So when I convert to csv file to table in matlab, the name of the file remains"
There is absolutely no reason why variables that are internal to your program have to have the same names as the files themselves. You could import them into one variable (i.e. better design) and then you entire question (and all of the slow and buggy solutions to it) is no longer needed.
Summary: fix the data import, rather than trying to create buggy code to fix the problems that the import causes.
Walter Roberson
on 26 Sep 2016
function S = name_of_variable(SomeVariable)
S = inputname(SomeVariable);
To use:
title( name_of_variable( cat ) )
and that would use 'cat' as the title.
2 Comments
Walter Roberson
on 27 Sep 2016
function S = name_of_variable(SomeVariable)
S = inputname(1);
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!