Plot with dot notation as an updating variable

4 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2021-11-25
编辑:Stephen23 2021-11-25
"%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.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2021-11-25
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 个评论
Hans Jorgen Jensen
Hans Jorgen Jensen 2021-11-25
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by