Plotting by string name

Hey all, simple question (I think). I have a matrix named sxx and one named t. I wanna plot(t,sxx) but I wanna use strings, meaning I have a string filename called 'sxx' and I wanna be able to plot(t,sprintf('%s',filename)). The latter of course doesn't work, since filename is a 1x3 char array whereas sxx is a Nx1 double matrix. Thanks!

Answers (2)

data = load(filename);
plot(t, data);
If the file is a .mat file and the variable stored in it is named "sxx" then,
data = load(filename);
plot(t, data.sxx)
If the file is a .mat file and the variable stored in it is the same as the .mat file name then,
data = load(filename);
plot(t, data.(filename));

4 Comments

Thanks for your answer. Actually the files are loaded into memory space, so the filename stuff I used is a bad example. Let me rephrase my question, in order to clarify things. My apologies for any confusion I might have created.
I have a matrix loaded into memory called sxx. How can I plot it via a string? Something like plot(t,sprintf(%s,nameofmatrix)) where nameofmatrix is a string nameofmatrix='sxx'. I want to do that, because I can include the name of the matrix in plot titles etc, and I can iterate through plotting sxx, syy, szz etc by just changing the string name. All matrices are already loaded into memory space.
Stephen23
Stephen23 on 5 Mar 2016
Edited: Stephen23 on 5 Mar 2016
Easy: don't store separate variables. Store one structure with fields, and access the fields using strings:
S.field1 = [...];
S.field2 = [...];
C = fieldnames(S);
for k = 1:numel(C)
S.(C{k}) % use a string to access each fields data
end
Whatever you do, do NOT try to access lots of varaible names dynamically. This is a slow and buggy way to write code, not matter how much beginners love to keep re-inventing it. Read this to know why you should not try to access your variables dynamically:
How did the variables get into your workspace?
Vas Nas
Vas Nas on 5 Mar 2016
Edited: Vas Nas on 5 Mar 2016
Ok you are right, and how can I assign an "alias" name to them? So I can use the name of the matrix for example sxx into plot titles etc? I can do plot(t,S.field2) but I would like to be able to assigned S.field2 to a string alias name for faster and easier access. I am using an import script that uses textscan and formatspec to read 10 columns from a file. I could replace that one I guess with load(filename) and load them into a struct with 10 fields? That's what you mean?
Stephen23
Stephen23 on 5 Mar 2016
Edited: Stephen23 on 5 Mar 2016
It is easy to refer to fieldnames using strings. The example in my first comment shows this: C is a cell array of the fieldnames (i.e. strings). Read this to know more about using strings to access fieldnames:
And try it yourself:
>> S.first = 3;
>> S.test = 'cat';
>> S.another = NaN;
>> S.('test')
ans =
cat
You could load your data into a variable, or use textscan. Either of these will work in a loop: use indexing into an ND array, or a cell array, or use dynamic fieldnames of a structure. It is up to you, and what suits you best.

Sign in to comment.

Regarding your clarification, I'll assume your strings are something you get somehow from your user, like they select a predefined name from a popup control, or they type in a name into an edit text control. So to plot the desired string variable, you can use strcmp
if strcmp(lower(theString), 'sxx')
plot(t, sxx, 'b*=', 'LineWidth', 2, 'MarkerSize', 14);
grid on;
elseif strcmp(lower(theString), 'y')
plot(t, y, 'b*=', 'LineWidth', 2, 'MarkerSize', 14);
grid on;
elseif strcmp(lower(theString), 'syy')
plot(t, syy, 'b*=', 'LineWidth', 2, 'MarkerSize', 14);
grid on;
elseif strcmp(lower(theString), 'whatever')
plot(t, whatever, 'b*=', 'LineWidth', 2, 'MarkerSize', 14);
grid on;
end

Categories

Products

Asked:

on 5 Mar 2016

Answered:

on 5 Mar 2016

Community Treasure Hunt

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

Start Hunting!