Plot with dot notation as an updating variable

1 view (last 30 days)
Hans Jorgen Jensen
Hans Jorgen Jensen on 25 Nov 2021
Edited: Stephen on 25 Nov 2021
How to make the plot command accept a variable name as part of the dot notation ?
See my code below
% Cam plots
% goal: to only have one plot file for several sets of data
%Data files (.mat)have different index, (DataFile1, DataFile2, etc)
%There are 3 sets of data in each data file.
%In DataFile1 it is Set11, Set12 and Set13
%In DataFile2 it is Set21, Set22 and Set23 , etc
%The variable names of the data columns are the same in all sets, meaning Set11,
%Set21, Set31, etc have same variable name (Dures,EMoteurTreuilAbar, etc)
%so the plot command must be able to use the updated data set names
close all
clear
load('DataFile1.mat'); % could also be DataFile2, DataFile3, etc
WS_vars = who('-file','DataFile1.mat');% Read names of data sets
VarToPlot=string(WS_vars{1}); % take the name of Set11
%I want the plot command to be able to use different values for 'VarToPlot'
%It could be 'Set11', 'Set21','Set31' etc.
%How to make that work with dot notation ?
%Below is shown what I want, but it throws an error
plot(VarToPlot.Dures,VarToPlot.EMoteurTreuilAbar);
The error I get:
>> TestPlot
Unrecognized method, property, or field 'Dures' for class 'string'.
Error in TestPlot (line 24)
plot(VarToPlot.Dures,VarToPlot.EMoteurTreuilAbar);
  1 Comment
Stephen
Stephen on 25 Nov 2021
"%In DataFile1 it is Set11, Set12 and Set13 %In DataFile2 it is Set21, Set22 and Set23 , etc"
And that is the start of your difficulties right there: putting meta-data (e.g. pseudo-indices, case numbers) into the variable names forces you into writing slower, more complex, less robust code.
In contrast if you used exactly the same variable names in each .mat file then your code would be simpler and more robust.

Sign in to comment.

Accepted Answer

Rik
Rik on 25 Nov 2021
You painted yourself in a corner when you decided to store data in variable names. However, you can still use your mat files. You need to load to a struct, instead of poofing all the variables to your workspace.
You should avoid storing data in variable names. A sign that you're doing something wrong is when you need to generate the variable name on the fly, instead of when you're writing your code.
%download the mat file from your question
websave('DataFile1.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/812884/DataFile1.mat');
%load to a struct
S=load('DataFile1.mat');
%retrieve all variable names
WS_vars = fieldnames(S);
%select a specific field from S, which is one of your variables
VarToPlot=S.(WS_vars{1});
%call plot
plot(VarToPlot.Dures,VarToPlot.EMoteurTreuilAbar);
  1 Comment
Hans Jorgen Jensen
Hans Jorgen Jensen on 25 Nov 2021
thank you very much, so simple and elegant :-)
All my data comes in Ecxel files, so I have the option to improve the way I handle it in Matlab. Can you point me in a direction where I can see how to avoid storing data in variable names, like you mention ?
Again, thanks for your help

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by