Extracting the name of my cell array to use for figure titles

23 次查看(过去 30 天)
Hello,
I have been searching for quite a while for a method to take the name of my cell and print it as the title of a plot.
For example: I have a 5x1 cell array named BeadsAll where each cell contains data from a different experiment trial. I want to throw the cell array BeadsAll into a function I have made that cleans, organizes, and plots the trial data and then properly titles it with respect to the cell array it is contained in.
Since I have a number of different cell arrays other than the "BeadsAll" that I want to use this function for, the title of the plots needs to reflect the cell array that is being called and evaluated.
I have heard that the "eval" function is not the way to go - and frankly - I'm still confused on how it's used even after looking at the documentation and forums.
Some guidance on a function I have overlooked or a process I can embed into my function that will convert the handle of my cell array to usable string for naming purposes would be really appreciated!
Thank you so much,
Christian
  1 个评论
Jiri Hajek
Jiri Hajek 2022-11-23
Hi, this should be fairly easy to accomplish. To give you an answer however, please first describe how you store the names of your cell arrays? The name "BeadsAll" and alike are read from a file or generated dynamicaly?

请先登录,再进行评论。

回答(2 个)

Robert U
Robert U 2022-11-23
Hi Christian Bundschu,
Have a look for structure arrays (struct()). With structure arrays you can manage your cell arrays not as single variables but as structure. It becomes handy in processing the cell arrays that could be stored as multiple fields with meaningful names.
You can read fieldnames with the command fieldnames() plus it is possible to name and address fieldnames dynamically (Generate Fieldnames from Variables).
Kind regards,
Robert

Stephen23
Stephen23 2022-11-23
编辑:Stephen23 2022-11-23
"Some guidance on a function I have overlooked ..."
Using EVAL() is a red-herring, and is very unlikely to help you with this task.
IWantThisNameInMyTitle = [1,4,6];
myplot(IWantThisNameInMyTitle)
function myplot(Y)
T = inputname(1);
plot(Y)
title(T)
end

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by