want to change what data I am plotting by text name.
7 views (last 30 days)
Show older comments
Riley Duffens
on 13 Jun 2023
Commented: Riley Duffens
on 13 Jun 2023
I am needing to plot many things from a .mat file but do not want to copy and paste the same code 30 times. I am wanting to set a varible such as 'CylinderPressure' and call it later. This works for everything but plotting. How can I change the 'CylinderPressure' so whats in it (the varible in question) can be referenced in plotting. In the end I want to just change the var variable to change what I am plotting. I atached the code I want to modify. Any help would be greatly apreciated.
var = 'CylinderPressure';
hold on
load('march.mat', var)
plot(CylinderPressure)
load('Feb.mat', var)
plot(CylinderPressure)
title(var)
legend('march','feb')
hold off
0 Comments
Accepted Answer
Stephen23
on 13 Jun 2023
Edited: Stephen23
on 13 Jun 2023
Do NOT load directly into the workspace, always LOAD into an output variable (which is a scalar structure).
Then simply use this syntax:
var = 'CylinderPressure';
S1 = load('march.mat',var);
S2 = load('Feb.mat' ,var);
V1 = S1.(var);
V2 = S2.(var);
hold on
plot(V1)
plot(V2)
title(var)
legend('march','feb')
hold off
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!