How to get original name of handle class object when stored inside a variable for batch processing

Hi All,
I'm processing a lot of experimental data stored in a handle class structure. I need to batch process the data in a loop using methods in the class. To identify figures which are generated I need a way to get the string of the original class handle.
classdef experimentClass <handle
methods
function plotData(obj)
plot(obj.data)
title(originalHandle)
savefig([originalHandle 'plotname'])
end
I need something which gives the originalHandle string. I call the method like this:
test1 = experimentClass;
test2 = experimentClass;
set = [test1; test2]; % list of objects to be batch processed
for i = 1:length(set)
set(i).plotData % plot
end
I'm looking for a way to do without reorganising my data as I have 100+ test objects. I am open to suggestions for better ways to do this in future though as this is messy.....
Many thanks

1 Comment

Where are your 100+ objects? Surely you haven't explicitly named them all test1...test100?!
I don't really get what you mean by 'the string of the original class handle' though. If you mean the variable name then, if you are unfortunate enough to have named them all individually surely it is just
[ 'test' num2str(i) ];

Sign in to comment.

Answers (1)

Storing important information that you intend to be used by the computer in the name of the variable is IMO a bad idea. Storing important information in the variable name isn't bad, if it's intended to be used by a person reading the code to understand what it does. For instance, calling a variable salesTaxRate rather than r stores information in the name, but the program shouldn't try to interpret that string and figure out what to do with it.
I recommend giving your experimentClass object a property, something like titleString, that the user sets when they instantiate the class. In the case where the user doesn't specify a value for that property, you could try to create a default using inputname but you would also need to consider what to do when the handle they pass into experimentClass doesn't have a name.
f = figure;
e1 = experimentClass(f); % the input has a name, f
e2 = experimentClass(figure); % the input does not have a name

Tags

Asked:

on 28 Apr 2017

Answered:

on 28 Apr 2017

Community Treasure Hunt

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

Start Hunting!